Skip to content

Commit

Permalink
fix(registry): CR
Browse files Browse the repository at this point in the history
  • Loading branch information
harnyk committed Oct 2, 2024
1 parent 6ef7f5b commit 5206989
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 39 deletions.
4 changes: 2 additions & 2 deletions registry/server/templates/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
46 changes: 19 additions & 27 deletions registry/server/templates/services/templatesRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export interface TemplatesGetListFilters {
name?: string[] | string;
}

//----

interface CreateTemplateResultOk {
type: 'ok';
template: VersionedRecord<TemplateWithLocalizedVersions>;
Expand All @@ -38,8 +36,6 @@ interface CreateTemplateResultLocalesNotSupported {

type CreateTemplateResult = CreateTemplateResultOk | CreateTemplateResultLocalesNotSupported;

//----

interface UpdateTemplateResultOk {
type: 'ok';
template: VersionedRecord<TemplateWithLocalizedVersions>;
Expand All @@ -59,8 +55,6 @@ type UpdateTemplateResult =
| UpdateTemplateResultNotFound
| UpdateTemplateResultLocalesNotSupported;

//----

interface UpsertTemplateLocalizedVersionResultOk {
type: 'ok';
localizedVersion: LocalizedVersion;
Expand All @@ -80,8 +74,6 @@ type UpsertTemplateLocalizedVersionResult =
| UpsertTemplateLocalizedVersionResultNotFound
| UpsertTemplateLocalizedVersionResultLocaleNotSupported;

//----

interface DeleteTemplateLocalizedVersionResultOk {
type: 'ok';
}
Expand All @@ -94,8 +86,6 @@ type DeleteTemplateLocalizedVersionResult =
| DeleteTemplateLocalizedVersionResultOk
| DeleteTemplateLocalizedVersionResultNotFound;

//----

export class TemplatesRepository {
constructor(private db: VersionedKnex) {}

Expand Down Expand Up @@ -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 };
}
Expand All @@ -194,9 +181,6 @@ export class TemplatesRepository {
): Promise<UpdateTemplateResult> {
const { db } = this;

const template = {
content: payload.content,
};
const templatesToUpdate = await db(Tables.Templates).where({
name: templateName,
});
Expand All @@ -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 };
}

Expand Down Expand Up @@ -282,6 +264,16 @@ export class TemplatesRepository {
return { type: 'ok' };
}

private async mustReadTemplateWithAllVersions(
templateName: string,
): Promise<VersionedRecord<TemplateWithLocalizedVersions>> {
const maybeTemplate = await this.readTemplateWithAllVersions(templateName);
if (!maybeTemplate) {
throw new Error('Template not found');
}
return maybeTemplate;
}

private async upsertLocalizedVersions(
templateName: string,
localizedVersions: Record<string, LocalizedVersion>,
Expand Down
20 changes: 10 additions & 10 deletions registry/tests/templates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
});
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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);
});
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 5206989

Please sign in to comment.