π₯ π₯ π₯ A Free & Self-hostable Airtable Alternative
TypeScript airtableairtable-alternativeautomatic-apihacktoberfestlow-codeno-codeno-code-databaseno-code-platformpostgresqlrest-apirestful-apispreadsheetsqliteswagger
handle duplicate view title
Fendy Heryanto2f2cd44255c1
diff --git a/packages/nocodb/src/services/v3/views-v3.service.ts b/packages/nocodb/src/services/v3/views-v3.service.tsindex 89dfc8ea5e..fc123ee902 100644--- a/packages/nocodb/src/services/v3/views-v3.service.ts+++ b/packages/nocodb/src/services/v3/views-v3.service.ts@@ -591,6 +591,16 @@ export class ViewsV3Service {'Missing view `title` property in request body',);}+ const existingViewTitle = await View.getByTitleOrId(+ context,+ { fk_model_id: tableId, titleOrId: body.title },+ ncMeta,+ );+ if (existingViewTitle) {+ NcError.get(context).invalidRequestBody(+ `View title '${body.title}' already exists`,+ );+ }const viewTypeCode =viewTypeMap[(body.type as any as string).toLowerCase()];@@ -1073,6 +1083,17 @@ export class ViewsV3Service {NcError.get(context).invalidRequestBody('Missing view `title` property in request body',);+ } else if ('title' in body && body.title !== existingView.title) {+ const existingViewTitle = await View.getByTitleOrId(+ context,+ { fk_model_id: existingView.fk_model_id, titleOrId: body.title },+ ncMeta,+ );+ if (existingViewTitle) {+ NcError.get(context).invalidRequestBody(+ `View title '${body.title}' already exists`,+ );+ }}const viewTypeCode = existingView.type;diff --git a/packages/nocodb/tests/unit/rest/tests/metaApiV3/error-handling/view.test.ts b/packages/nocodb/tests/unit/rest/tests/metaApiV3/error-handling/view.test.tsindex a895172fa5..c69d454c2e 100644--- a/packages/nocodb/tests/unit/rest/tests/metaApiV3/error-handling/view.test.ts+++ b/packages/nocodb/tests/unit/rest/tests/metaApiV3/error-handling/view.test.ts@@ -120,6 +120,55 @@ export default function () {'Missing view `title` property in request body',);});+ it(`will handle duplicate name`, async () => {+ const response1 = await request(context.app)+ .post(`${API_PREFIX}/tables/${table.id}/views`)+ .set('xc-token', context.xc_token)+ .send({+ title: 'MyView',+ type: 'grid',+ });+ const response = await request(context.app)+ .post(`${API_PREFIX}/tables/${table.id}/views`)+ .set('xc-token', context.xc_token)+ .send({+ title: 'MyView',+ type: 'grid',+ });+ expect(response.status).to.eq(400);+ expect(response.body.error).to.eq('INVALID_REQUEST_BODY');+ expect(response.body.message).to.eq(+ `View title 'MyView' already exists`,+ );+ const response2 = await request(context.app)+ .post(`${API_PREFIX}/tables/${table.id}/views`)+ .set('xc-token', context.xc_token)+ .send({+ title: 'MyView32',+ type: 'grid',+ });+ expect(response2.status).to.eq(200);+ const updateResponse1 = await request(context.app)+ .patch(`${API_PREFIX}/views/${response2.body.id}`)+ .set('xc-token', context.xc_token)+ .send({+ title: 'MyView32',+ type: 'grid',+ });+ expect(updateResponse1.status).to.eq(200);+ const updateResponse = await request(context.app)+ .patch(`${API_PREFIX}/views/${response2.body.id}`)+ .set('xc-token', context.xc_token)+ .send({+ title: 'MyView',+ type: 'grid',+ });+ expect(updateResponse.status).to.eq(400);+ expect(updateResponse.body.error).to.eq('INVALID_REQUEST_BODY');+ expect(updateResponse.body.message).to.eq(+ `View title 'MyView' already exists`,+ );+ });it(`will handle empty type`, async () => {const response = await request(context.app).post(`${API_PREFIX}/tables/${table.id}/views`)