mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Function for updateContribution.
This commit is contained in:
parent
03db4ed125
commit
7b38fe7f93
@ -2,7 +2,7 @@ import { RIGHTS } from '@/auth/RIGHTS'
|
||||
import { Context, getUser } from '@/server/context'
|
||||
import { backendLogger as logger } from '@/server/logger'
|
||||
import { Contribution as dbContribution } from '@entity/Contribution'
|
||||
import { Arg, Args, Authorized, Ctx, Mutation, Query, Resolver } from 'type-graphql'
|
||||
import { Arg, Args, Authorized, Ctx, Int, Mutation, Query, Resolver } from 'type-graphql'
|
||||
import { IsNull } from '../../../../database/node_modules/typeorm'
|
||||
import ContributionArgs from '../arg/ContributionArgs'
|
||||
import Paginated from '../arg/Paginated'
|
||||
@ -10,7 +10,7 @@ import { Order } from '../enum/Order'
|
||||
import { Contribution } from '../model/Contribution'
|
||||
import { UnconfirmedContribution } from '../model/UnconfirmedContribution'
|
||||
import { User } from '../model/User'
|
||||
import { isContributionValid, getUserCreation } from './util/isContributionValid'
|
||||
import { isContributionValid, getUserCreation, updateCreations } from './util/isContributionValid'
|
||||
|
||||
@Resolver()
|
||||
export class ContributionResolver {
|
||||
@ -75,4 +75,41 @@ export class ContributionResolver {
|
||||
}
|
||||
return contribution.map((contr) => new Contribution(contr, new User(user)))
|
||||
}
|
||||
|
||||
@Authorized([RIGHTS.UPDATE_CONTRIBUTION])
|
||||
@Mutation(() => UnconfirmedContribution)
|
||||
async updateContribution(
|
||||
@Arg('contributionId', () => Int)
|
||||
contributionId: number,
|
||||
@Args() { amount, memo, creationDate }: ContributionArgs,
|
||||
@Ctx() context: Context,
|
||||
): Promise<UnconfirmedContribution> {
|
||||
const user = getUser(context)
|
||||
|
||||
const contributionToUpdate = await dbContribution.findOne({
|
||||
where: { id: contributionId, confirmedAt: IsNull() },
|
||||
})
|
||||
if (!contributionToUpdate) {
|
||||
throw new Error('No contribution found to given id.')
|
||||
}
|
||||
if (contributionToUpdate.userId !== user.id) {
|
||||
throw new Error('user of the pending contribution and send user does not correspond')
|
||||
}
|
||||
|
||||
const creationDateObj = new Date(creationDate)
|
||||
let creations = await getUserCreation(user.id)
|
||||
if (contributionToUpdate.contributionDate.getMonth() === creationDateObj.getMonth()) {
|
||||
creations = updateCreations(creations, contributionToUpdate)
|
||||
}
|
||||
|
||||
// all possible cases not to be true are thrown in this function
|
||||
isContributionValid(creations, amount, creationDateObj)
|
||||
contributionToUpdate.amount = amount
|
||||
contributionToUpdate.memo = memo
|
||||
contributionToUpdate.contributionDate = new Date(creationDate)
|
||||
dbContribution.save(contributionToUpdate)
|
||||
|
||||
const result = new UnconfirmedContribution(contributionToUpdate, user, creations)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user