From dee4cee82ff17bf4fa81adbe217bc1671aea4b9f Mon Sep 17 00:00:00 2001 From: TULCHINSKI LIRAN Date: Sun, 29 Mar 2026 15:31:14 +0300 Subject: [PATCH 1/5] fix: if record already exists expecet 400 --- openapi3.yaml | 1 + src/openapi.d.ts | 1 + src/records/controllers/recordsController.ts | 1 + src/validations/models/validationsManager.ts | 2 +- tests/integration/records/records.spec.ts | 4 ++-- tests/unit/users/models/usersModel.spec.ts | 2 +- 6 files changed, 7 insertions(+), 4 deletions(-) diff --git a/openapi3.yaml b/openapi3.yaml index 16d6d1a..6afc029 100644 --- a/openapi3.yaml +++ b/openapi3.yaml @@ -759,6 +759,7 @@ components: type: string enum: - SUCCESS + - RECORD_ALREADY_EXIST - MISSING_CREDENTIALS - MISSING_COORDINATES - INVALID_COORDINATES_OR_DISTANCE diff --git a/src/openapi.d.ts b/src/openapi.d.ts index e30ef8e..e321881 100644 --- a/src/openapi.d.ts +++ b/src/openapi.d.ts @@ -132,6 +132,7 @@ export type components = { /** @enum {string} */ code: | 'SUCCESS' + | 'RECORD_ALREADY_EXIST' | 'MISSING_CREDENTIALS' | 'MISSING_COORDINATES' | 'INVALID_COORDINATES_OR_DISTANCE' diff --git a/src/records/controllers/recordsController.ts b/src/records/controllers/recordsController.ts index 7ef3cb4..3c93c35 100644 --- a/src/records/controllers/recordsController.ts +++ b/src/records/controllers/recordsController.ts @@ -224,6 +224,7 @@ export class RecordsController { const code = result.code ?? 'UNKNOWN'; switch (code) { case 'MISSING_CREDENTIALS': + case 'RECORD_ALREADY_EXIST': return httpStatus.BAD_REQUEST; case 'INVALID_RECORD_NAME': case 'INVALID_RECORD_NAME_ANOTHER_SITE': diff --git a/src/validations/models/validationsManager.ts b/src/validations/models/validationsManager.ts index 36338fe..7f250c8 100644 --- a/src/validations/models/validationsManager.ts +++ b/src/validations/models/validationsManager.ts @@ -51,7 +51,7 @@ export class ValidationsManager { const record = await this.extractableRepo.findOne({ where: { record_name: payload.recordName } }); if (record) { this.logger.debug({ msg: 'record already exists for create', recordName: payload.recordName, logContext }); - return { isValid: false, message: `Record '${payload.recordName}' already exists`, code: 'INVALID_RECORD_NAME' }; + return { isValid: false, message: `Record '${payload.recordName}' already exists`, code: 'RECORD_ALREADY_EXIST' }; } let existsAndPublishedInCatalog: boolean; diff --git a/tests/integration/records/records.spec.ts b/tests/integration/records/records.spec.ts index 0b5b309..82e9dde 100644 --- a/tests/integration/records/records.spec.ts +++ b/tests/integration/records/records.spec.ts @@ -286,11 +286,11 @@ describe('records', function () { }, }); - expect(response.status).toBe(httpStatusCodes.NOT_FOUND); + expect(response.status).toBe(httpStatusCodes.BAD_REQUEST); expect(response.body).toEqual({ isValid: false, message: `Record 'rec_name' already exists`, - code: 'INVALID_RECORD_NAME', + code: 'RECORD_ALREADY_EXIST', }); }); }); diff --git a/tests/unit/users/models/usersModel.spec.ts b/tests/unit/users/models/usersModel.spec.ts index 2c5fce7..e02ffae 100644 --- a/tests/unit/users/models/usersModel.spec.ts +++ b/tests/unit/users/models/usersModel.spec.ts @@ -97,7 +97,7 @@ describe('ValidationsManager - User & Record Validation', () => { expect(result.isValid).toBe(false); expect(result.message).toBe("Record 'existingRecord' already exists"); - expect(result.code).toBe('INVALID_RECORD_NAME'); + expect(result.code).toBe('RECORD_ALREADY_EXIST'); }); describe('Remote Validation', () => { From 3935b6cfcfcf049de32e2fa3b3410174769149c6 Mon Sep 17 00:00:00 2001 From: TULCHINSKI LIRAN Date: Sun, 29 Mar 2026 15:57:10 +0300 Subject: [PATCH 2/5] fix: if record already exists expecet 400 --- tests/integration/records/records.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/records/records.spec.ts b/tests/integration/records/records.spec.ts index 82e9dde..c213222 100644 --- a/tests/integration/records/records.spec.ts +++ b/tests/integration/records/records.spec.ts @@ -261,7 +261,7 @@ describe('records', function () { }); expect(response).toSatisfyApiSpec(); - expect(response.status).toBe(httpStatusCodes.NOT_FOUND); + expect(response.status).toBe(httpStatusCodes.BAD_REQUEST); }); describe('Duplicate Creation', function () { @@ -276,7 +276,7 @@ describe('records', function () { }); }); - it('should return 404 when createRecord throws "Record not found"', async () => { + it('should return 400 when createRecord throws "Record not found"', async () => { const response = await requestSender.createRecord({ pathParams: { recordName: validCredentials.recordName }, requestBody: { From df48905c5f5289eb9ed9725c0cd33e4d22b0672f Mon Sep 17 00:00:00 2001 From: TULCHINSKI LIRAN Date: Sun, 29 Mar 2026 16:12:03 +0300 Subject: [PATCH 3/5] chore: changed error names of inavlid record names --- openapi3.yaml | 14 +++++++------- src/openapi.d.ts | 4 ++-- src/records/controllers/recordsController.ts | 8 ++++---- src/validations/models/validationsManager.ts | 6 +++--- tests/integration/records/records.spec.ts | 14 +++++++------- tests/unit/records/models/recordsModel.spec.ts | 4 ++-- tests/unit/users/models/usersModel.spec.ts | 10 +++++----- 7 files changed, 30 insertions(+), 30 deletions(-) diff --git a/openapi3.yaml b/openapi3.yaml index 92ea23c..6b5f363 100644 --- a/openapi3.yaml +++ b/openapi3.yaml @@ -205,7 +205,7 @@ paths: value: isValid: false message: Record with name 'rec_X' not found - code: INVALID_RECORD_NAME + code: RECORD_NAME_NOT_FOUND '500': description: Internal server error content: @@ -312,7 +312,7 @@ paths: value: isValid: false message: Record with name 'rec_X' not found - code: INVALID_RECORD_NAME + code: RECORD_NAME_NOT_FOUND '500': description: Internal server error content: @@ -399,7 +399,7 @@ paths: value: isValid: false message: Cannot delete – record 'rec_X' not found - code: INVALID_RECORD_NAME + code: RECORD_NAME_NOT_FOUND '500': description: Internal server error content: @@ -487,7 +487,7 @@ paths: value: isValid: false message: Record 'rec_name' already exists - code: INVALID_RECORD_NAME + code: RECORD_NAME_NOT_FOUND '500': description: Internal server error content: @@ -565,7 +565,7 @@ paths: value: isValid: false message: Record 'rec_name' doesn't exists - code: INVALID_RECORD_NAME + code: RECORD_NAME_NOT_FOUND '500': description: Internal server error content: @@ -764,8 +764,8 @@ components: - MISSING_COORDINATES - INVALID_COORDINATES_OR_DISTANCE - INVALID_CREDENTIALS - - INVALID_RECORD_NAME - - INVALID_RECORD_NAME_ANOTHER_SITE + - RECORD_NAME_NOT_FOUND + - RECORD_NAME_NOT_FOUND_ANOTHER_SITE - INTERNAL_ERROR - INVALID_START_POSITION - INVALID_MAX_RECORDS diff --git a/src/openapi.d.ts b/src/openapi.d.ts index e321881..976c051 100644 --- a/src/openapi.d.ts +++ b/src/openapi.d.ts @@ -137,8 +137,8 @@ export type components = { | 'MISSING_COORDINATES' | 'INVALID_COORDINATES_OR_DISTANCE' | 'INVALID_CREDENTIALS' - | 'INVALID_RECORD_NAME' - | 'INVALID_RECORD_NAME_ANOTHER_SITE' + | 'RECORD_NAME_NOT_FOUND' + | 'RECORD_NAME_NOT_FOUND_ANOTHER_SITE' | 'INTERNAL_ERROR' | 'INVALID_START_POSITION' | 'INVALID_MAX_RECORDS'; diff --git a/src/records/controllers/recordsController.ts b/src/records/controllers/recordsController.ts index 3c93c35..f9a3e29 100644 --- a/src/records/controllers/recordsController.ts +++ b/src/records/controllers/recordsController.ts @@ -95,7 +95,7 @@ export class RecordsController { const record = await this.manager.getRecord(recordName); if (!record) { - return res.status(httpStatus.NOT_FOUND).json({ isValid: false, message: `Record ${recordName} not found`, code: 'INVALID_RECORD_NAME' }); + return res.status(httpStatus.NOT_FOUND).json({ isValid: false, message: `Record ${recordName} not found`, code: 'RECORD_NAME_NOT_FOUND' }); } return res.status(httpStatus.OK).json(record); @@ -165,7 +165,7 @@ export class RecordsController { if (!deleted) { this.requestsCounter.inc({ status: '404' }); - return res.status(httpStatus.NOT_FOUND).json({ isValid: false, message: `Record ${recordName} not found`, code: 'INVALID_RECORD_NAME' }); + return res.status(httpStatus.NOT_FOUND).json({ isValid: false, message: `Record ${recordName} not found`, code: 'RECORD_NAME_NOT_FOUND' }); } this.requestsCounter.inc({ status: '204' }); @@ -226,8 +226,8 @@ export class RecordsController { case 'MISSING_CREDENTIALS': case 'RECORD_ALREADY_EXIST': return httpStatus.BAD_REQUEST; - case 'INVALID_RECORD_NAME': - case 'INVALID_RECORD_NAME_ANOTHER_SITE': + case 'RECORD_NAME_NOT_FOUND': + case 'RECORD_NAME_NOT_FOUND_ANOTHER_SITE': return httpStatus.NOT_FOUND; case 'INVALID_CREDENTIALS': return httpStatus.UNAUTHORIZED; diff --git a/src/validations/models/validationsManager.ts b/src/validations/models/validationsManager.ts index 7f250c8..8bb5514 100644 --- a/src/validations/models/validationsManager.ts +++ b/src/validations/models/validationsManager.ts @@ -64,7 +64,7 @@ export class ValidationsManager { if (!existsAndPublishedInCatalog) { this.logger.debug({ msg: 'record does not exist in catalog', recordName: payload.recordName, logContext }); - return { isValid: false, message: `Record '${payload.recordName}' is missing from the catalog`, code: 'INVALID_RECORD_NAME' }; + return { isValid: false, message: `Record '${payload.recordName}' is missing from the catalog`, code: 'RECORD_NAME_NOT_FOUND' }; } this.logger.debug({ msg: `multiSiteValidation ${payload.multiSiteValidation}`, logContext }); @@ -97,7 +97,7 @@ export class ValidationsManager { const validOnOtherSites = results.every(Boolean); if (!validOnOtherSites) { this.logger.debug({ msg: 'record validation failed on another site', recordName: payload.recordName, logContext }); - return { isValid: false, message: 'Record validation failed on another site', code: 'INVALID_RECORD_NAME_ANOTHER_SITE' }; + return { isValid: false, message: 'Record validation failed on another site', code: 'RECORD_NAME_NOT_FOUND_ANOTHER_SITE' }; } } catch (err) { this.logger.warn({ msg: 'remote validation unavailable', recordName: payload.recordName, logContext, err }); @@ -125,7 +125,7 @@ export class ValidationsManager { const record = await this.extractableRepo.findOne({ where: { record_name: payload.recordName } }); if (!record) { this.logger.debug({ msg: 'record not found for delete', record_name: payload.recordName, logContext }); - return { isValid: false, message: `Record '${payload.recordName}' not found`, code: 'INVALID_RECORD_NAME' }; + return { isValid: false, message: `Record '${payload.recordName}' not found`, code: 'RECORD_NAME_NOT_FOUND' }; } this.logger.debug({ msg: 'delete validation successful', recordName: payload.recordName, logContext }); diff --git a/tests/integration/records/records.spec.ts b/tests/integration/records/records.spec.ts index c213222..7697179 100644 --- a/tests/integration/records/records.spec.ts +++ b/tests/integration/records/records.spec.ts @@ -521,7 +521,7 @@ describe('records', function () { expect(response.body).toEqual({ isValid: false, message: `Record ${invalidCredentials.recordName} not found`, - code: 'INVALID_RECORD_NAME', + code: 'RECORD_NAME_NOT_FOUND', }); }); @@ -534,11 +534,11 @@ describe('records', function () { expect(response.status).toBe(httpStatusCodes.NOT_FOUND); }); - it('should return 404 when validateCreate returns INVALID_RECORD_NAME', async () => { + it('should return 404 when validateCreate returns RECORD_NAME_NOT_FOUND', async () => { jest.spyOn(ValidationsManager.prototype, 'validateCreate').mockResolvedValueOnce({ isValid: false, message: 'Record does not exist', - code: 'INVALID_RECORD_NAME', + code: 'RECORD_NAME_NOT_FOUND', }); const response = await requestSender.validateCreate({ @@ -553,17 +553,17 @@ describe('records', function () { expect(response.body).toEqual({ isValid: false, message: 'Record does not exist', - code: 'INVALID_RECORD_NAME', + code: 'RECORD_NAME_NOT_FOUND', }); jest.restoreAllMocks(); }); - it('should return 404 when validateDelete returns INVALID_RECORD_NAME', async () => { + it('should return 404 when validateDelete returns RECORD_NAME_NOT_FOUND', async () => { jest.spyOn(ValidationsManager.prototype, 'validateDelete').mockResolvedValueOnce({ isValid: false, message: 'Record does not exist', - code: 'INVALID_RECORD_NAME', + code: 'RECORD_NAME_NOT_FOUND', }); const response = await requestSender.validateDelete({ @@ -578,7 +578,7 @@ describe('records', function () { expect(response.body).toEqual({ isValid: false, message: 'Record does not exist', - code: 'INVALID_RECORD_NAME', + code: 'RECORD_NAME_NOT_FOUND', }); jest.restoreAllMocks(); diff --git a/tests/unit/records/models/recordsModel.spec.ts b/tests/unit/records/models/recordsModel.spec.ts index 59bac0e..f29148f 100644 --- a/tests/unit/records/models/recordsModel.spec.ts +++ b/tests/unit/records/models/recordsModel.spec.ts @@ -273,7 +273,7 @@ describe('RecordsManager & ValidationsManager', () => { code: 'INTERNAL_ERROR', }); }); - it('should return INVALID_RECORD_NAME if catalog does not contain record', async () => { + it('should return RECORD_NAME_NOT_FOUND if catalog does not contain record', async () => { (mockCatalogCall as unknown as CatalogCall).findPublishedRecord = jest.fn().mockResolvedValueOnce(false); const result = await validationsManager.validateCreate({ @@ -284,7 +284,7 @@ describe('RecordsManager & ValidationsManager', () => { expect(result).toEqual({ isValid: false, message: "Record 'missingRecord' is missing from the catalog", - code: 'INVALID_RECORD_NAME', + code: 'RECORD_NAME_NOT_FOUND', }); }); }); diff --git a/tests/unit/users/models/usersModel.spec.ts b/tests/unit/users/models/usersModel.spec.ts index e02ffae..55617fa 100644 --- a/tests/unit/users/models/usersModel.spec.ts +++ b/tests/unit/users/models/usersModel.spec.ts @@ -167,7 +167,7 @@ describe('ValidationsManager - User & Record Validation', () => { const routes = [{ url: 'http://site1.com' }, { url: 'http://site2.com' }]; mockedAxios.post .mockResolvedValueOnce({ data: { isValid: true, code: 'SUCCESS', message: 'Valid' } }) - .mockResolvedValueOnce({ data: { isValid: false, code: 'INVALID_RECORD_NAME', message: 'Invalid' } }); + .mockResolvedValueOnce({ data: { isValid: false, code: 'RECORD_NAME_NOT_FOUND', message: 'Invalid' } }); validationsManager = createValidationsManager({ get: (key: string) => { @@ -183,7 +183,7 @@ describe('ValidationsManager - User & Record Validation', () => { expect(result.isValid).toBe(false); expect(result.message).toBe('Record validation failed on another site'); - expect(result.code).toBe('INVALID_RECORD_NAME_ANOTHER_SITE'); + expect(result.code).toBe('RECORD_NAME_NOT_FOUND_ANOTHER_SITE'); }); it('should return false for remote site when fetch fails', async () => { @@ -206,7 +206,7 @@ describe('ValidationsManager - User & Record Validation', () => { expect(result.isValid).toBe(false); expect(result.message).toBe('Record validation failed on another site'); - expect(result.code).toBe('INVALID_RECORD_NAME_ANOTHER_SITE'); + expect(result.code).toBe('RECORD_NAME_NOT_FOUND_ANOTHER_SITE'); }); it('should handle remote validation config retrieval failure (routes load failure at construction)', async () => { @@ -247,7 +247,7 @@ describe('ValidationsManager - User & Record Validation', () => { expect(result.isValid).toBe(false); expect(result.message).toBe('Record validation failed on another site'); - expect(result.code).toBe('INVALID_RECORD_NAME_ANOTHER_SITE'); + expect(result.code).toBe('RECORD_NAME_NOT_FOUND_ANOTHER_SITE'); }); }); }); @@ -272,7 +272,7 @@ describe('ValidationsManager - User & Record Validation', () => { expect(result.isValid).toBe(false); expect(result.message).toBe("Record 'nonExistingRecord' not found"); - expect(result.code).toBe('INVALID_RECORD_NAME'); + expect(result.code).toBe('RECORD_NAME_NOT_FOUND'); }); }); }); From 3b38738da8f895d0d70e4b37872af563efdd2d01 Mon Sep 17 00:00:00 2001 From: TULCHINSKI LIRAN Date: Sun, 29 Mar 2026 16:14:56 +0300 Subject: [PATCH 4/5] chore: changed error names of inavlid record names --- openapi3.yaml | 2 +- src/openapi.d.ts | 2 +- src/records/controllers/recordsController.ts | 2 +- src/validations/models/validationsManager.ts | 2 +- tests/integration/records/records.spec.ts | 2 +- tests/unit/users/models/usersModel.spec.ts | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/openapi3.yaml b/openapi3.yaml index 6b5f363..f5dfa19 100644 --- a/openapi3.yaml +++ b/openapi3.yaml @@ -759,7 +759,7 @@ components: type: string enum: - SUCCESS - - RECORD_ALREADY_EXIST + - RECORD_NAME_ALREADY_EXIST - MISSING_CREDENTIALS - MISSING_COORDINATES - INVALID_COORDINATES_OR_DISTANCE diff --git a/src/openapi.d.ts b/src/openapi.d.ts index 976c051..4441400 100644 --- a/src/openapi.d.ts +++ b/src/openapi.d.ts @@ -132,7 +132,7 @@ export type components = { /** @enum {string} */ code: | 'SUCCESS' - | 'RECORD_ALREADY_EXIST' + | 'RECORD_NAME_ALREADY_EXIST' | 'MISSING_CREDENTIALS' | 'MISSING_COORDINATES' | 'INVALID_COORDINATES_OR_DISTANCE' diff --git a/src/records/controllers/recordsController.ts b/src/records/controllers/recordsController.ts index f9a3e29..171b26f 100644 --- a/src/records/controllers/recordsController.ts +++ b/src/records/controllers/recordsController.ts @@ -224,7 +224,7 @@ export class RecordsController { const code = result.code ?? 'UNKNOWN'; switch (code) { case 'MISSING_CREDENTIALS': - case 'RECORD_ALREADY_EXIST': + case 'RECORD_NAME_ALREADY_EXIST': return httpStatus.BAD_REQUEST; case 'RECORD_NAME_NOT_FOUND': case 'RECORD_NAME_NOT_FOUND_ANOTHER_SITE': diff --git a/src/validations/models/validationsManager.ts b/src/validations/models/validationsManager.ts index 8bb5514..31a4316 100644 --- a/src/validations/models/validationsManager.ts +++ b/src/validations/models/validationsManager.ts @@ -51,7 +51,7 @@ export class ValidationsManager { const record = await this.extractableRepo.findOne({ where: { record_name: payload.recordName } }); if (record) { this.logger.debug({ msg: 'record already exists for create', recordName: payload.recordName, logContext }); - return { isValid: false, message: `Record '${payload.recordName}' already exists`, code: 'RECORD_ALREADY_EXIST' }; + return { isValid: false, message: `Record '${payload.recordName}' already exists`, code: 'RECORD_NAME_ALREADY_EXIST' }; } let existsAndPublishedInCatalog: boolean; diff --git a/tests/integration/records/records.spec.ts b/tests/integration/records/records.spec.ts index 7697179..ee56595 100644 --- a/tests/integration/records/records.spec.ts +++ b/tests/integration/records/records.spec.ts @@ -290,7 +290,7 @@ describe('records', function () { expect(response.body).toEqual({ isValid: false, message: `Record 'rec_name' already exists`, - code: 'RECORD_ALREADY_EXIST', + code: 'RECORD_NAME_ALREADY_EXIST', }); }); }); diff --git a/tests/unit/users/models/usersModel.spec.ts b/tests/unit/users/models/usersModel.spec.ts index 55617fa..c2711db 100644 --- a/tests/unit/users/models/usersModel.spec.ts +++ b/tests/unit/users/models/usersModel.spec.ts @@ -97,7 +97,7 @@ describe('ValidationsManager - User & Record Validation', () => { expect(result.isValid).toBe(false); expect(result.message).toBe("Record 'existingRecord' already exists"); - expect(result.code).toBe('RECORD_ALREADY_EXIST'); + expect(result.code).toBe('RECORD_NAME_ALREADY_EXIST'); }); describe('Remote Validation', () => { From d7bfc5b934458f0fe1e917e9b07c6be704f55683 Mon Sep 17 00:00:00 2001 From: TULCHINSKI LIRAN Date: Sun, 29 Mar 2026 16:24:14 +0300 Subject: [PATCH 5/5] chore: changed error names of inavlid record names --- openapi3.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi3.yaml b/openapi3.yaml index f5dfa19..7c526f9 100644 --- a/openapi3.yaml +++ b/openapi3.yaml @@ -487,7 +487,7 @@ paths: value: isValid: false message: Record 'rec_name' already exists - code: RECORD_NAME_NOT_FOUND + code: RECORD_NAME_ALREADY_EXIST '500': description: Internal server error content: