Skip to content

Commit

Permalink
⚡ uuid error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
maspio committed Jan 18, 2023
1 parent 3f104b8 commit c04e8b7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
37 changes: 27 additions & 10 deletions packages/nodes-base/nodes/Notion/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
INodeProperties,
IPollFunctions,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';

import { camelCase, capitalCase, snakeCase } from 'change-case';
Expand All @@ -25,14 +26,19 @@ import moment from 'moment-timezone';

import { validate as uuidValidate } from 'uuid';

const uuidValidateWithoutDashes = (value: string) => {
function uuidValidateWithoutDashes(this: IExecuteFunctions, value: string) {
if (!value || typeof value !== 'string') return false;
if (uuidValidate(value)) return true;
if (value.length !== 32) return false;
//prettier-ignore
const strWithDashes = `${value.slice(0, 8)}-${value.slice(8, 12)}-${value.slice(12, 16)}-${value.slice(16, 20)}-${value.slice(20)}`;
return uuidValidate(strWithDashes);
};
if (value.length == 32) {
//prettier-ignore
const strWithDashes = `${value.slice(0, 8)}-${value.slice(8, 12)}-${value.slice(12, 16)}-${value.slice(16, 20)}-${value.slice(20)}`;
if (uuidValidate(strWithDashes)) return true;
}
throw new NodeOperationError(
this.getNode(),
`The relation id "${value}" is not a valid uuid with optional dashes.`,
);
}

export type SortData = { key: string; type: string; direction: string; timestamp: boolean };

Expand Down Expand Up @@ -272,7 +278,13 @@ function getDateFormat(includeTime: boolean) {
return '';
}

function getPropertyKeyValue(value: any, type: string, timezone: string, version = 1) {
function getPropertyKeyValue(
this: IExecuteFunctions,
value: any,
type: string,
timezone: string,
version = 1,
) {
const ignoreIfEmpty = <T>(v: T, cb: (v: T) => any) =>
!v && value.ignoreIfEmpty ? undefined : cb(v);
let result: IDataObject = {};
Expand Down Expand Up @@ -302,7 +314,7 @@ function getPropertyKeyValue(value: any, type: string, timezone: string, version
type: 'relation',
relation: value.relationValue
.filter((rv: string) => {
return uuidValidateWithoutDashes(rv);
return uuidValidateWithoutDashes.call(this, rv);
})
.reduce((acc: [], cur: any) => {
return acc.concat(cur.split(',').map((relation: string) => ({ id: relation.trim() })));
Expand Down Expand Up @@ -406,7 +418,12 @@ function getNameAndType(key: string) {
};
}

export function mapProperties(properties: IDataObject[], timezone: string, version = 1) {
export function mapProperties(
this: IExecuteFunctions,
properties: IDataObject[],
timezone: string,
version = 1,
) {
return properties
.filter(
(property): property is Record<string, { key: string; [k: string]: any }> =>
Expand All @@ -416,7 +433,7 @@ export function mapProperties(properties: IDataObject[], timezone: string, versi
(property) =>
[
`${property.key.split('|')[0]}`,
getPropertyKeyValue(property, property.key.split('|')[1], timezone, version),
getPropertyKeyValue.call(this, property, property.key.split('|')[1], timezone, version),
] as const,
)
.filter(([, value]) => value)
Expand Down
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/Notion/v1/NotionV1.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ export class NotionV1 implements INodeType {
[],
) as IDataObject[];
if (properties.length !== 0) {
body.properties = mapProperties(properties, timezone) as IDataObject;
body.properties = mapProperties.call(this, properties, timezone) as IDataObject;
}
const blockValues = this.getNodeParameter('blockUi.blockValues', i, []) as IDataObject[];
extractDatabaseMentionRLC(blockValues);
Expand Down Expand Up @@ -460,7 +460,7 @@ export class NotionV1 implements INodeType {
properties: {},
};
if (properties.length !== 0) {
body.properties = mapProperties(properties, timezone) as IDataObject;
body.properties = mapProperties.call(this, properties, timezone) as IDataObject;
}
responseData = await notionApiRequest.call(this, 'PATCH', `/pages/${pageId}`, body);
if (simple) {
Expand Down
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/Notion/v2/NotionV2.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ export class NotionV2 implements INodeType {
if (propertiesValues.length !== 0) {
body.properties = Object.assign(
body.properties,
mapProperties(propertiesValues, timezone, 2) as IDataObject,
mapProperties.call(this, propertiesValues, timezone, 2) as IDataObject,
);
}
const blockValues = this.getNodeParameter('blockUi.blockValues', i, []) as IDataObject[];
Expand Down Expand Up @@ -590,7 +590,7 @@ export class NotionV2 implements INodeType {
properties: {},
};
if (properties.length !== 0) {
body.properties = mapProperties(properties, timezone, 2) as IDataObject;
body.properties = mapProperties.call(this, properties, timezone, 2) as IDataObject;
}
responseData = await notionApiRequest.call(this, 'PATCH', `/pages/${pageId}`, body);
if (simple) {
Expand Down

0 comments on commit c04e8b7

Please sign in to comment.