From 574e1a40ef3c0848abf7c44c8d5a3eebb6b51b74 Mon Sep 17 00:00:00 2001 From: LWangllix Date: Mon, 5 Feb 2024 21:03:38 +0200 Subject: [PATCH] minor fixes --- mixins/database.mixin.ts | 12 --------- services/forms.service.ts | 51 ++++++++++++++++++++++----------------- services/users.service.ts | 11 +++------ utils/mails.ts | 2 +- 4 files changed, 33 insertions(+), 43 deletions(-) diff --git a/mixins/database.mixin.ts b/mixins/database.mixin.ts index c4784fb..12d3b20 100644 --- a/mixins/database.mixin.ts +++ b/mixins/database.mixin.ts @@ -46,18 +46,6 @@ export default function (opts: any = {}) { return; }, - async update(ctx: any) { - return this.updateEntity( - ctx, - { - ...ctx.params, - }, - { - ...ctx.options, - } - ); - }, - async removeAllEntities(ctx: any) { return await this.clearEntities(ctx); }, diff --git a/services/forms.service.ts b/services/forms.service.ts index 1308dbb..f8c3ed8 100644 --- a/services/forms.service.ts +++ b/services/forms.service.ts @@ -225,6 +225,12 @@ const populatePermissions = (field: string) => { if (entity.assigneeId === value) return value; + const error = 'Assignee cannot be set.'; + + if (user.type === UserType.USER) { + throwBadRequestError(error); + } + const availableAssigneeList: { rows: User[] } = await ctx.call( 'forms.getAssignees' ); @@ -232,8 +238,8 @@ const populatePermissions = (field: string) => { (assignee) => assignee.id === Number(value) ); - if (user.type === UserType.USER || !assigneeAuthUser) { - throwBadRequestError('Assignee cannot be set.'); + if (!assigneeAuthUser) { + throwBadRequestError(error); } const localUser: User = await ctx.call('users.findOrCreate', { @@ -399,6 +405,8 @@ export default class FormsService extends moleculer.Service { ], total: 1, page: 1, + pageSize: 10, + totalPages: 1, }; return await ctx.call('auth.users.list', { @@ -410,7 +418,6 @@ export default class FormsService extends moleculer.Service { }, }, fields: ['id', 'firstName', 'lastName', 'phone', 'email', 'type'], - pageSize: 99999, }); } @@ -544,7 +551,7 @@ export default class FormsService extends moleculer.Service { let canAssign = true; - if (isCreatedByUser || authUser?.type === UserType.USER) { + if (isCreatedByUser || user?.type === UserType.USER) { canAssign = false; } @@ -685,6 +692,24 @@ export default class FormsService extends moleculer.Service { async 'forms.updated'(ctx: Context>) { const { oldData: prevForm, data: form } = ctx.params; + if (prevForm?.status !== form.status) { + const { comment } = ctx.options?.parentCtx?.params as any; + const typesByStatus = { + [FormStatus.SUBMITTED]: FormHistoryTypes.UPDATED, + [FormStatus.REJECTED]: FormHistoryTypes.REJECTED, + [FormStatus.RETURNED]: FormHistoryTypes.RETURNED, + [FormStatus.APPROVED]: FormHistoryTypes.APPROVED, + }; + + await ctx.call('forms.histories.create', { + form: form.id, + comment, + type: typesByStatus[form.status], + }); + + await this.sendNotificationOnStatusChange(form); + } + if (!!form?.assignee && prevForm?.assignee !== form?.assignee) { const assignee: User = await ctx.call('users.resolve', { id: form.assignee, @@ -706,24 +731,6 @@ export default class FormsService extends moleculer.Service { object.id ); } - - if (prevForm?.status !== form.status) { - const { comment } = ctx.options?.parentCtx?.params as any; - const typesByStatus = { - [FormStatus.SUBMITTED]: FormHistoryTypes.UPDATED, - [FormStatus.REJECTED]: FormHistoryTypes.REJECTED, - [FormStatus.RETURNED]: FormHistoryTypes.RETURNED, - [FormStatus.APPROVED]: FormHistoryTypes.APPROVED, - }; - - await ctx.call('forms.histories.create', { - form: form.id, - comment, - type: typesByStatus[form.status], - }); - - await this.sendNotificationOnStatusChange(form); - } } @Event() diff --git a/services/users.service.ts b/services/users.service.ts index bd848a1..d9850fd 100644 --- a/services/users.service.ts +++ b/services/users.service.ts @@ -379,14 +379,9 @@ export default class UsersService extends moleculer.Service { }; if (user?.id) { - return ctx.call( - 'users.update', - { - id: user.id, - ...dataToSave, - }, - { scope } as any - ); + return this.updateEntity(ctx, dataToSave, { + scope, + }); } // let user to customize his phone and email diff --git a/utils/mails.ts b/utils/mails.ts index cfaa040..76a2d97 100644 --- a/utils/mails.ts +++ b/utils/mails.ts @@ -41,7 +41,7 @@ export function notifyFormAssignee( To: email.toLowerCase(), TemplateId: 34487978, TemplateModel: { - actionUrl: `${hostUrl()}/uetk/teikimo-anketos/${formId}`, + actionUrl: `${hostUrl(true)}/uetk/teikimo-anketos/${formId}`, typeText: formTypeTranslates[formType] || 'teikimo', objectName, },