From 25dececb2a5210ec36391223ba2ba0c633a00712 Mon Sep 17 00:00:00 2001 From: Mayur Date: Fri, 15 Nov 2024 19:45:17 +0530 Subject: [PATCH 1/6] feat: Thrown error if duplicate keys found --- .../valid-request/valid-request.usecase.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts b/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts index 82041682..ed0b974e 100644 --- a/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts +++ b/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts @@ -55,9 +55,14 @@ export class ValidRequest { ); } - const columnKeysSet = new Set(parsedSchema.map((column) => column.key)); + const columnKeys = parsedSchema.map((column) => column.key); + const columnKeysSet = new Set(columnKeys); + const duplicateKeys = columnKeys.filter((key, index) => columnKeys.indexOf(key) !== index); + if (columnKeysSet.size !== parsedSchema.length) { - throw new UniqueColumnException(APIMessages.COLUMN_KEY_TAKEN); + throw new UniqueColumnException( + `${APIMessages.COLUMN_KEY_DUPLICATED} Duplicate Keys Found - ${duplicateKeys.join(', ')}` + ); } for (const item of parsedSchema) { @@ -115,6 +120,16 @@ export class ValidRequest { return { success: true }; } catch (error) { + if (error instanceof UniqueColumnException) { + throw new HttpException( + { + message: error.message, + errorCode: error.getStatus(), + }, + HttpStatus.UNPROCESSABLE_ENTITY + ); + } + if (error instanceof DocumentNotFoundException) { throw new HttpException( { From 30fe7cd7ba558be389fb72c9ea1fe04d637357c6 Mon Sep 17 00:00:00 2001 From: Mayur Date: Fri, 15 Nov 2024 19:45:54 +0530 Subject: [PATCH 2/6] feat: Changed APIMessage COLUMN_KEY_DUPLICATED --- apps/api/src/app/shared/constants.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/api/src/app/shared/constants.ts b/apps/api/src/app/shared/constants.ts index 194d5529..c988898e 100644 --- a/apps/api/src/app/shared/constants.ts +++ b/apps/api/src/app/shared/constants.ts @@ -30,7 +30,7 @@ export const APIMessages = { INVALID_AUTH_TOKEN: 'Invalid authentication token', INVALID_RSS_URL: 'The Specified URL doesn`t contain any RSS XML Feed, Please enter a Valid RSS XML URL', COLUMN_KEY_TAKEN: 'Column with the same key already exists. Please provide a unique key from "Validations".', - COLUMN_KEY_DUPLICATED: 'Column with the same key already exists. Please provide a unique key.', + COLUMN_KEY_DUPLICATED: 'Column with the same key already exists. Please use a unique key in column.', ERROR_DURING_VALIDATION: 'Something went wrong while validating data. Data is not imported yet, but team is informed about issue. Please try again after sometime.', FEATURE_UNAVAILABLE: { From 71882632b3a58b33e92f79b51a2862cd561658ac Mon Sep 17 00:00:00 2001 From: Mayur Date: Fri, 15 Nov 2024 19:51:42 +0530 Subject: [PATCH 3/6] feat: Provided error in exception --- .../app/common/usecases/valid-request/valid-request.usecase.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts b/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts index ed0b974e..589ed6c3 100644 --- a/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts +++ b/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts @@ -61,7 +61,7 @@ export class ValidRequest { if (columnKeysSet.size !== parsedSchema.length) { throw new UniqueColumnException( - `${APIMessages.COLUMN_KEY_DUPLICATED} Duplicate Keys Found - ${duplicateKeys.join(', ')}` + `${APIMessages.COLUMN_KEY_DUPLICATED} Duplicate Keys Found for ${duplicateKeys.join(', ')}` ); } From 35233c8264f3b2e2a2e8b8887100c57cb92aca93 Mon Sep 17 00:00:00 2001 From: Mayur Date: Sat, 16 Nov 2024 14:38:39 +0530 Subject: [PATCH 4/6] feat: Removed extra filter while having parsedSchema --- .../usecases/valid-request/valid-request.usecase.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts b/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts index 589ed6c3..07f2bffe 100644 --- a/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts +++ b/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts @@ -55,10 +55,13 @@ export class ValidRequest { ); } - const columnKeys = parsedSchema.map((column) => column.key); - const columnKeysSet = new Set(columnKeys); - const duplicateKeys = columnKeys.filter((key, index) => columnKeys.indexOf(key) !== index); + const columnKeysSet = new Set(); + const duplicateKeys = parsedSchema.reduce((acc, item) => { + if (columnKeysSet.has(item.key)) acc.push(item.key); + else columnKeysSet.add(item.key); + return acc; + }, []); if (columnKeysSet.size !== parsedSchema.length) { throw new UniqueColumnException( `${APIMessages.COLUMN_KEY_DUPLICATED} Duplicate Keys Found for ${duplicateKeys.join(', ')}` From 13e78edf0ce93883eaefef4b28f5f879dad72964 Mon Sep 17 00:00:00 2001 From: Mayur Date: Sat, 16 Nov 2024 14:59:05 +0530 Subject: [PATCH 5/6] feat: Shown each duplicated key only once --- .../usecases/valid-request/valid-request.usecase.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts b/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts index 07f2bffe..840780bf 100644 --- a/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts +++ b/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts @@ -57,14 +57,16 @@ export class ValidRequest { const columnKeysSet = new Set(); const duplicateKeys = parsedSchema.reduce((acc, item) => { - if (columnKeysSet.has(item.key)) acc.push(item.key); - else columnKeysSet.add(item.key); + if (columnKeysSet.has(item.key) && !acc.includes(item.key)) { + acc.push(item.key); + } + columnKeysSet.add(item.key); return acc; }, []); if (columnKeysSet.size !== parsedSchema.length) { throw new UniqueColumnException( - `${APIMessages.COLUMN_KEY_DUPLICATED} Duplicate Keys Found for ${duplicateKeys.join(', ')}` + `${APIMessages.COLUMN_KEY_DUPLICATED} Duplicate Keys Found for ${duplicateKeys.join(', ')}` ); } From f14fa628075ada6730f44d98b55b04cd7663019d Mon Sep 17 00:00:00 2001 From: Mayur Date: Sat, 16 Nov 2024 15:04:07 +0530 Subject: [PATCH 6/6] feat: Taken new Set --- .../usecases/valid-request/valid-request.usecase.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts b/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts index 840780bf..3cf01f80 100644 --- a/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts +++ b/apps/api/src/app/common/usecases/valid-request/valid-request.usecase.ts @@ -57,16 +57,14 @@ export class ValidRequest { const columnKeysSet = new Set(); const duplicateKeys = parsedSchema.reduce((acc, item) => { - if (columnKeysSet.has(item.key) && !acc.includes(item.key)) { - acc.push(item.key); - } + if (columnKeysSet.has(item.key)) acc.add(item.key); columnKeysSet.add(item.key); return acc; - }, []); + }, new Set()); if (columnKeysSet.size !== parsedSchema.length) { throw new UniqueColumnException( - `${APIMessages.COLUMN_KEY_DUPLICATED} Duplicate Keys Found for ${duplicateKeys.join(', ')}` + `${APIMessages.COLUMN_KEY_DUPLICATED} Duplicate Keys Found for ${[...duplicateKeys].join(', ')}` ); }