From 289d2bb000149a00fafd07230bda0e391fac446e Mon Sep 17 00:00:00 2001 From: Daniele Guido Date: Sat, 11 Jan 2025 15:57:12 +0100 Subject: [PATCH] Fix/user change plan request (#486) * Update user-change-plan-request.class.ts * add checks for already existing user-group relationships --------- Co-authored-by: Daniele Guido <1181642+danieleguido@users.noreply.github.com> --- .../user-change-plan-request.class.ts | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/services/user-change-plan-request/user-change-plan-request.class.ts b/src/services/user-change-plan-request/user-change-plan-request.class.ts index 08667d90..c67ea949 100644 --- a/src/services/user-change-plan-request/user-change-plan-request.class.ts +++ b/src/services/user-change-plan-request/user-change-plan-request.class.ts @@ -2,10 +2,10 @@ import type { Sequelize } from 'sequelize' import initDebug from 'debug' import type { ImpressoApplication } from '../../types' import User from '../../models/users.model' -import { NotFound } from '@feathersjs/errors' +import { BadRequest, NotFound } from '@feathersjs/errors' import UserChangePlanRequest from '../../models/user-change-plan-request' -const debug = initDebug('impresso:services/change-plan') +const debug = initDebug('impresso:services/user-change-plan-request') export interface ServiceOptions { app: ImpressoApplication @@ -41,13 +41,30 @@ export class Service { return userChangePlanRequest?.get() } - async create(data: any, params: { user: { id: number } }) { + async create(data: { plan: string }, params: { user: { id: number; groups: string[] } }) { const client = this.app.get('celeryClient') if (!client) { throw new Error('Celery client not available') } - + if (!this.sequelizeClient) { + throw new Error('Sequelize client not available') + } debug('[create] plan request for user.pk', params.user.id, 'plan:', data.plan, params.user) + // check if the plan selected is already included in params.user.groups + if (params.user.groups?.includes(data.plan)) { + throw new BadRequest('User is already granted access to the requested plan.', { plan: 'Already granted' }) + } + // check if the user is already in the process of changing the plan + const userChangePlanRequestModel = UserChangePlanRequest.initModel(this.sequelizeClient) + const userChangePlanRequest = await userChangePlanRequestModel.findOne({ + where: { + userId: params.user.id, + }, + }) + if (userChangePlanRequest) { + // return the existing request as data in BadRequest error + throw new BadRequest('User is already in the process of changing the plan', userChangePlanRequest?.get()) + } return client .run({