Skip to content

Commit

Permalink
Fix/user change plan request (#486)
Browse files Browse the repository at this point in the history
* Update user-change-plan-request.class.ts

* add checks for already existing user-group relationships

---------

Co-authored-by: Daniele Guido <[email protected]>
  • Loading branch information
danieleguido and danieleguido authored Jan 11, 2025
1 parent 2d5d21f commit 289d2bb
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 289d2bb

Please sign in to comment.