diff --git a/registry/server/templates/routes/index.ts b/registry/server/templates/routes/index.ts index 26cf13f2..7fafcd26 100644 --- a/registry/server/templates/routes/index.ts +++ b/registry/server/templates/routes/index.ts @@ -18,8 +18,8 @@ export default (authMw: RequestHandler[]) => { templatesRouter.get('/:name', ...getTemplate); templatesRouter.put('/:name', authMw, ...updateTemplate); templatesRouter.delete('/:name', authMw, ...deleteTemplate); - templatesRouter.put('/:name/localizedVersions/:locale', authMw, ...upsertTemplateLocalizedVersion); - templatesRouter.delete('/:name/localizedVersions/:locale', authMw, ...deleteTemplateLocalizedVersion); + templatesRouter.put('/:name/localized/:locale', authMw, ...upsertTemplateLocalizedVersion); + templatesRouter.delete('/:name/localized/:locale', authMw, ...deleteTemplateLocalizedVersion); return templatesRouter; }; diff --git a/registry/server/templates/services/templatesRepository.ts b/registry/server/templates/services/templatesRepository.ts index f11e8899..2c784e33 100644 --- a/registry/server/templates/services/templatesRepository.ts +++ b/registry/server/templates/services/templatesRepository.ts @@ -24,8 +24,6 @@ export interface TemplatesGetListFilters { name?: string[] | string; } -//---- - interface CreateTemplateResultOk { type: 'ok'; template: VersionedRecord; @@ -38,8 +36,6 @@ interface CreateTemplateResultLocalesNotSupported { type CreateTemplateResult = CreateTemplateResultOk | CreateTemplateResultLocalesNotSupported; -//---- - interface UpdateTemplateResultOk { type: 'ok'; template: VersionedRecord; @@ -59,8 +55,6 @@ type UpdateTemplateResult = | UpdateTemplateResultNotFound | UpdateTemplateResultLocalesNotSupported; -//---- - interface UpsertTemplateLocalizedVersionResultOk { type: 'ok'; localizedVersion: LocalizedVersion; @@ -80,8 +74,6 @@ type UpsertTemplateLocalizedVersionResult = | UpsertTemplateLocalizedVersionResultNotFound | UpsertTemplateLocalizedVersionResultLocaleNotSupported; -//---- - interface DeleteTemplateLocalizedVersionResultOk { type: 'ok'; } @@ -94,8 +86,6 @@ type DeleteTemplateLocalizedVersionResult = | DeleteTemplateLocalizedVersionResultOk | DeleteTemplateLocalizedVersionResultNotFound; -//---- - export class TemplatesRepository { constructor(private db: VersionedKnex) {} @@ -175,14 +165,11 @@ export class TemplatesRepository { await db.versioning(user, { type: EntityTypes.templates, id: template.name }, async (trx) => { await db(Tables.Templates).insert(templateToCreate).transacting(trx); if (Object.keys(localizedVersions).length > 0) { - await templatesRepository.upsertLocalizedVersions(template.name, localizedVersions, trx); + await this.upsertLocalizedVersions(template.name, localizedVersions, trx); } }); - const savedTemplate = await templatesRepository.readTemplateWithAllVersions(template.name); - if (!savedTemplate) { - throw new Error('Failed to create template'); - } + const savedTemplate = await this.mustReadTemplateWithAllVersions(template.name); return { type: 'ok', template: savedTemplate }; } @@ -194,9 +181,6 @@ export class TemplatesRepository { ): Promise { const { db } = this; - const template = { - content: payload.content, - }; const templatesToUpdate = await db(Tables.Templates).where({ name: templateName, }); @@ -216,18 +200,16 @@ export class TemplatesRepository { id: templateName, }, async (trx) => { - await db(Tables.Templates).where({ name: templateName }).update(template).transacting(trx); - await templatesRepository.upsertLocalizedVersions(templateName, localizedVersions, trx); + await db(Tables.Templates) + .where({ name: templateName }) + .update({ content: payload.content }) + .transacting(trx); + await this.upsertLocalizedVersions(templateName, localizedVersions, trx); }, ); - const updatedTemplate = await templatesRepository.readTemplateWithAllVersions(templateName); - if (!updatedTemplate) { - // This time we cannot simply return UpdateTemplateResultNotFound result, - // because the template is assumed to have been just updated, - // so it must exist. - throw new Error(`Template ${templateName} not found`); - } + const updatedTemplate = await this.mustReadTemplateWithAllVersions(templateName); + return { type: 'ok', template: updatedTemplate }; } @@ -282,6 +264,16 @@ export class TemplatesRepository { return { type: 'ok' }; } + private async mustReadTemplateWithAllVersions( + templateName: string, + ): Promise> { + const maybeTemplate = await this.readTemplateWithAllVersions(templateName); + if (!maybeTemplate) { + throw new Error('Template not found'); + } + return maybeTemplate; + } + private async upsertLocalizedVersions( templateName: string, localizedVersions: Record, diff --git a/registry/tests/templates.spec.ts b/registry/tests/templates.spec.ts index 0dd87ce9..b8843fc8 100644 --- a/registry/tests/templates.spec.ts +++ b/registry/tests/templates.spec.ts @@ -779,7 +779,7 @@ describe(`Tests ${example.url}`, () => { await req.post(example.url).send(example.correct).expect(200); await req - .put(example.url + example.correct.name + '/localizedVersions/pt-BR') + .put(example.url + example.correct.name + '/localized/pt-BR') .send({ content: example.correct.content }) .expect(422); } finally { @@ -790,7 +790,7 @@ describe(`Tests ${example.url}`, () => { const nonExistentTemplate = 'non-existent-template'; await req - .put(example.url + nonExistentTemplate + '/localizedVersions/ua-UA') + .put(example.url + nonExistentTemplate + '/localized/ua-UA') .send({ content: example.correct.content }) .expect(404); }); @@ -799,7 +799,7 @@ describe(`Tests ${example.url}`, () => { await req.post(example.url).send(example.correct).expect(200); await req - .put(example.url + example.correct.name + '/localizedVersions/ua-UA') + .put(example.url + example.correct.name + '/localized/ua-UA') .send({ content: example.correct.content }) .expect(200); } finally { @@ -813,11 +813,11 @@ describe(`Tests ${example.url}`, () => { try { await req.post(example.url).send(example.correct).expect(200); await req - .put(example.url + example.correct.name + '/localizedVersions/ua-UA') + .put(example.url + example.correct.name + '/localized/ua-UA') .send({ content: example.correct.content }) .expect(200); await req - .put(example.url + example.correct.name + '/localizedVersions/ua-UA') + .put(example.url + example.correct.name + '/localized/ua-UA') .send({ content: example.updated.content }) .expect(200); } finally { @@ -831,7 +831,7 @@ describe(`Tests ${example.url}`, () => { const nonExistentTemplate = 'non-existent-template'; await req - .put(example.url + nonExistentTemplate + '/localizedVersions/ua-UA') + .put(example.url + nonExistentTemplate + '/localized/ua-UA') .send({ content: example.correct.content }) .expect(404); }); @@ -840,10 +840,10 @@ describe(`Tests ${example.url}`, () => { try { await req.post(example.url).send(example.correct).expect(200); await req - .put(example.url + example.correct.name + '/localizedVersions/ua-UA') + .put(example.url + example.correct.name + '/localized/ua-UA') .send({ content: example.correct.content }) .expect(200); - await req.delete(example.url + example.correct.name + '/localizedVersions/en-US').expect(404); + await req.delete(example.url + example.correct.name + '/localized/en-US').expect(404); } finally { await req.delete(example.url + example.correct.name).expect(204); } @@ -853,10 +853,10 @@ describe(`Tests ${example.url}`, () => { try { await req.post(example.url).send(example.correct).expect(200); await req - .put(example.url + example.correct.name + '/localizedVersions/ua-UA') + .put(example.url + example.correct.name + '/localized/ua-UA') .send({ content: example.correct.content }) .expect(200); - await req.delete(example.url + example.correct.name + '/localizedVersions/ua-UA').expect(204); + await req.delete(example.url + example.correct.name + '/localized/ua-UA').expect(204); } finally { await req.delete(example.url + example.correct.name).expect(204); }