mirror of
https://github.com/IT4Change/gradido.git
synced 2026-03-01 12:44:43 +00:00
53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import { Contribution } from 'database'
|
|
import { Decimal } from 'decimal.js-light'
|
|
|
|
import {
|
|
getUserCreation,
|
|
updateCreations,
|
|
validateContribution,
|
|
} from '@/graphql/resolver/util/creations'
|
|
import { LogError } from '@/server/LogError'
|
|
|
|
export class ContributionLogic {
|
|
// how much gradido can be still created
|
|
private availableCreationSums?: Decimal[]
|
|
public constructor(private self: Contribution) {}
|
|
|
|
/**
|
|
* retrieve from db and return available creation sums array
|
|
* @param clientTimezoneOffset
|
|
* @param putThisBack if true, amount from this contribution will be added back to the availableCreationSums array,
|
|
* as if this creation wasn't part of it, used for update contribution
|
|
* @returns
|
|
*/
|
|
public async getAvailableCreationSums(
|
|
clientTimezoneOffset: number,
|
|
putThisBack = false,
|
|
): Promise<Decimal[]> {
|
|
// TODO: move code from getUserCreation and updateCreations inside this function/class
|
|
this.availableCreationSums = await getUserCreation(this.self.userId, clientTimezoneOffset)
|
|
if (putThisBack) {
|
|
this.availableCreationSums = updateCreations(
|
|
this.availableCreationSums,
|
|
this.self,
|
|
clientTimezoneOffset,
|
|
)
|
|
}
|
|
return this.availableCreationSums
|
|
}
|
|
|
|
public checkAvailableCreationSumsNotExceeded(
|
|
amount: Decimal,
|
|
creationDate: Date,
|
|
clientTimezoneOffset: number,
|
|
): void {
|
|
if (!this.availableCreationSums) {
|
|
throw new LogError(
|
|
'missing available creation sums, please call getAvailableCreationSums first',
|
|
)
|
|
}
|
|
// all possible cases not to be true are thrown in this function
|
|
validateContribution(this.availableCreationSums, amount, creationDate, clientTimezoneOffset)
|
|
}
|
|
}
|