fix: normalize optional json payload fields

This commit is contained in:
Jesse Freitas 2026-03-16 09:06:02 -03:00
parent 7cdba2e77e
commit 6bb07609a3
3 changed files with 129 additions and 42 deletions

View file

@ -5088,6 +5088,39 @@ export class Mega implements INodeType {
});
};
const parseOptionalObject = (
value: unknown,
itemIndex: number,
parameterName: string,
): IDataObject => {
if (value === undefined || value === null || value === '') {
return {};
}
let parsedValue = value;
if (typeof parsedValue === 'string') {
try {
parsedValue = JSON.parse(parsedValue);
} catch {
throw new NodeOperationError(
this.getNode(),
`${parameterName} must be a valid JSON object`,
{ itemIndex },
);
}
}
if (typeof parsedValue !== 'object' || Array.isArray(parsedValue)) {
throw new NodeOperationError(
this.getNode(),
`${parameterName} must be a JSON object`,
{ itemIndex },
);
}
return parsedValue as IDataObject;
};
const appendFormValue = (
formData: { append(name: string, value: unknown, fileName?: string): void },
key: string,
@ -5142,22 +5175,28 @@ export class Mega implements INodeType {
'messageCreateContentAttributes',
itemIndex,
{},
) as IDataObject;
const templateParams = this.getNodeParameter(
'messageCreateTemplateParams',
itemIndex,
{},
) as IDataObject;
);
const templateParams = this.getNodeParameter('messageCreateTemplateParams', itemIndex, {});
const campaignId = this.getNodeParameter('messageCreateCampaignId', itemIndex, 0) as number;
const parsedContentAttributes = parseOptionalObject(
contentAttributes,
itemIndex,
'messageCreateContentAttributes',
);
const parsedTemplateParams = parseOptionalObject(
templateParams,
itemIndex,
'messageCreateTemplateParams',
);
if (trimmedContent) {
body.content = trimmedContent;
}
if (Object.keys(contentAttributes).length > 0) {
body.content_attributes = contentAttributes;
if (Object.keys(parsedContentAttributes).length > 0) {
body.content_attributes = parsedContentAttributes;
}
if (Object.keys(templateParams).length > 0) {
body.template_params = templateParams;
if (Object.keys(parsedTemplateParams).length > 0) {
body.template_params = parsedTemplateParams;
}
if (campaignId > 0) {
body.campaign_id = campaignId;
@ -5218,12 +5257,18 @@ export class Mega implements INodeType {
'additionalAttributes',
itemIndex,
{},
) as IDataObject;
const customAttributes = this.getNodeParameter(
'customAttributes',
);
const customAttributes = this.getNodeParameter('customAttributes', itemIndex, {});
const parsedAdditionalAttributes = parseOptionalObject(
additionalAttributes,
itemIndex,
{},
) as IDataObject;
'additionalAttributes',
);
const parsedCustomAttributes = parseOptionalObject(
customAttributes,
itemIndex,
'customAttributes',
);
if (assigneeId > 0) {
body.assignee_id = assigneeId;
@ -5235,12 +5280,12 @@ export class Mega implements INodeType {
};
}
if (Object.keys(additionalAttributes).length > 0) {
body.additional_attributes = additionalAttributes;
if (Object.keys(parsedAdditionalAttributes).length > 0) {
body.additional_attributes = parsedAdditionalAttributes;
}
if (Object.keys(customAttributes).length > 0) {
body.custom_attributes = customAttributes;
if (Object.keys(parsedCustomAttributes).length > 0) {
body.custom_attributes = parsedCustomAttributes;
}
return body;
@ -5856,7 +5901,12 @@ export class Mega implements INodeType {
'scheduledMessageTemplateParams',
itemIndex,
{},
) as IDataObject;
);
const parsedTemplateParams = parseOptionalObject(
templateParams,
itemIndex,
'scheduledMessageTemplateParams',
);
const recurrenceType = this.getNodeParameter(
'scheduledMessageRecurrenceType',
itemIndex,
@ -5869,8 +5919,8 @@ export class Mega implements INodeType {
if (title.trim()) {
body.title = title;
}
if (Object.keys(templateParams).length > 0) {
body.template_params = templateParams;
if (Object.keys(parsedTemplateParams).length > 0) {
body.template_params = parsedTemplateParams;
}
if (recurrenceType !== 'none') {
@ -5995,7 +6045,11 @@ export class Mega implements INodeType {
body.scheduled_at = updateFields.scheduledAt;
}
if (updateFields.templateParams !== undefined) {
body.template_params = updateFields.templateParams;
body.template_params = parseOptionalObject(
updateFields.templateParams,
itemIndex,
'scheduledMessageUpdateFields.values.templateParams',
);
}
if (updateFields.title !== undefined && updateFields.title !== '') {
body.title = updateFields.title;
@ -6147,23 +6201,33 @@ export class Mega implements INodeType {
'contactAdditionalAttributes',
itemIndex,
{},
) as IDataObject;
);
const customAttributes = this.getNodeParameter(
'contactCustomAttributes',
itemIndex,
{},
) as IDataObject;
);
const parsedAdditionalAttributes = parseOptionalObject(
additionalAttributes,
itemIndex,
'contactAdditionalAttributes',
);
const parsedCustomAttributes = parseOptionalObject(
customAttributes,
itemIndex,
'contactCustomAttributes',
);
if (name.trim()) body.name = name;
if (email.trim()) body.email = email;
if (phoneNumber.trim()) body.phone_number = phoneNumber;
if (avatarUrl.trim()) body.avatar_url = avatarUrl;
if (identifier.trim()) body.identifier = identifier;
if (Object.keys(additionalAttributes).length > 0) {
body.additional_attributes = additionalAttributes;
if (Object.keys(parsedAdditionalAttributes).length > 0) {
body.additional_attributes = parsedAdditionalAttributes;
}
if (Object.keys(customAttributes).length > 0) {
body.custom_attributes = customAttributes;
if (Object.keys(parsedCustomAttributes).length > 0) {
body.custom_attributes = parsedCustomAttributes;
}
response = (await megaApiRequest.call(
@ -6189,7 +6253,11 @@ export class Mega implements INodeType {
const body: IDataObject = {};
if (updateFields.additionalAttributes !== undefined) {
body.additional_attributes = updateFields.additionalAttributes;
body.additional_attributes = parseOptionalObject(
updateFields.additionalAttributes,
itemIndex,
'contactUpdateFields.values.additionalAttributes',
);
}
if (updateFields.avatarUrl !== undefined && updateFields.avatarUrl !== '') {
body.avatar_url = updateFields.avatarUrl;
@ -6198,7 +6266,11 @@ export class Mega implements INodeType {
body.blocked = updateFields.blocked;
}
if (updateFields.customAttributes !== undefined) {
body.custom_attributes = updateFields.customAttributes;
body.custom_attributes = parseOptionalObject(
updateFields.customAttributes,
itemIndex,
'contactUpdateFields.values.customAttributes',
);
}
if (updateFields.email !== undefined && updateFields.email !== '') {
body.email = updateFields.email;
@ -6468,10 +6540,15 @@ export class Mega implements INodeType {
'campaignTemplateParams',
itemIndex,
{},
) as IDataObject;
);
const parsedTemplateParams = parseOptionalObject(
templateParams,
itemIndex,
'campaignTemplateParams',
);
if (Object.keys(templateParams).length > 0) {
body.template_params = templateParams;
if (Object.keys(parsedTemplateParams).length > 0) {
body.template_params = parsedTemplateParams;
}
response = (await megaApiRequest.call(
@ -6634,14 +6711,19 @@ export class Mega implements INodeType {
'chatRoomMessageContentAttributes',
itemIndex,
{},
) as IDataObject;
);
const parsedContentAttributes = parseOptionalObject(
contentAttributes,
itemIndex,
'chatRoomMessageContentAttributes',
);
if (echoId.trim()) {
chatRoomMessage.echo_id = echoId;
}
if (Object.keys(contentAttributes).length > 0) {
chatRoomMessage.content_attributes = contentAttributes;
if (Object.keys(parsedContentAttributes).length > 0) {
chatRoomMessage.content_attributes = parsedContentAttributes;
}
response = (await megaApiRequest.call(
@ -7273,13 +7355,18 @@ export class Mega implements INodeType {
'conversationCustomAttributesPayload',
itemIndex,
{},
) as IDataObject;
);
const parsedCustomAttributes = parseOptionalObject(
customAttributes,
itemIndex,
'conversationCustomAttributesPayload',
);
response = (await megaApiRequest.call(
this,
'POST',
`/api/v1/accounts/${accountId}/conversations/${conversationId}/custom_attributes`,
{
custom_attributes: customAttributes,
custom_attributes: parsedCustomAttributes,
},
)) as IDataObject;
} else if (resource === 'conversation' && operation === 'getLabels') {

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "@jessefreitas/n8n-nodes-mega",
"version": "0.4.9",
"version": "0.4.10",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@jessefreitas/n8n-nodes-mega",
"version": "0.4.9",
"version": "0.4.10",
"license": "MIT",
"devDependencies": {
"@n8n/node-cli": "0.23.0",

View file

@ -1,6 +1,6 @@
{
"name": "@jessefreitas/n8n-nodes-mega",
"version": "0.4.9",
"version": "0.4.10",
"description": "Trabalhe com a API do Mega",
"license": "MIT",
"homepage": "https://github.com/jessefreitas/n8n_community_mega",