mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
backend fix types for admin_pending_creations is now decimal
This commit is contained in:
parent
c3c3e556a0
commit
56ed35d692
@ -1,4 +1,5 @@
|
|||||||
import { ArgsType, Field, Float, InputType, Int } from 'type-graphql'
|
import { ArgsType, Field, InputType, Int } from 'type-graphql'
|
||||||
|
import Decimal from 'decimal.js-light'
|
||||||
|
|
||||||
@InputType()
|
@InputType()
|
||||||
@ArgsType()
|
@ArgsType()
|
||||||
@ -6,8 +7,8 @@ export default class CreatePendingCreationArgs {
|
|||||||
@Field(() => String)
|
@Field(() => String)
|
||||||
email: string
|
email: string
|
||||||
|
|
||||||
@Field(() => Float)
|
@Field(() => Decimal)
|
||||||
amount: number
|
amount: Decimal
|
||||||
|
|
||||||
@Field(() => String)
|
@Field(() => String)
|
||||||
memo: string
|
memo: string
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import { ArgsType, Field, Float, Int } from 'type-graphql'
|
import { ArgsType, Field, Int } from 'type-graphql'
|
||||||
|
import Decimal from 'decimal.js-light'
|
||||||
|
|
||||||
@ArgsType()
|
@ArgsType()
|
||||||
export default class UpdatePendingCreationArgs {
|
export default class UpdatePendingCreationArgs {
|
||||||
@ -8,8 +9,8 @@ export default class UpdatePendingCreationArgs {
|
|||||||
@Field(() => String)
|
@Field(() => String)
|
||||||
email: string
|
email: string
|
||||||
|
|
||||||
@Field(() => Float)
|
@Field(() => Decimal)
|
||||||
amount: number
|
amount: Decimal
|
||||||
|
|
||||||
@Field(() => String)
|
@Field(() => String)
|
||||||
memo: string
|
memo: string
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import { ObjectType, Field, Int } from 'type-graphql'
|
import { ObjectType, Field, Int } from 'type-graphql'
|
||||||
|
import Decimal from 'decimal.js-light'
|
||||||
|
|
||||||
@ObjectType()
|
@ObjectType()
|
||||||
export class PendingCreation {
|
export class PendingCreation {
|
||||||
@ -23,12 +24,12 @@ export class PendingCreation {
|
|||||||
@Field(() => String)
|
@Field(() => String)
|
||||||
memo: string
|
memo: string
|
||||||
|
|
||||||
@Field(() => Number)
|
@Field(() => Decimal)
|
||||||
amount: number
|
amount: Decimal
|
||||||
|
|
||||||
@Field(() => Number)
|
@Field(() => Number)
|
||||||
moderator: number
|
moderator: number
|
||||||
|
|
||||||
@Field(() => [Number])
|
@Field(() => [Decimal])
|
||||||
creation: number[]
|
creation: Decimal[]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import { ObjectType, Field } from 'type-graphql'
|
import { ObjectType, Field } from 'type-graphql'
|
||||||
|
import Decimal from 'decimal.js-light'
|
||||||
|
|
||||||
@ObjectType()
|
@ObjectType()
|
||||||
export class UpdatePendingCreation {
|
export class UpdatePendingCreation {
|
||||||
@ -8,12 +9,12 @@ export class UpdatePendingCreation {
|
|||||||
@Field(() => String)
|
@Field(() => String)
|
||||||
memo: string
|
memo: string
|
||||||
|
|
||||||
@Field(() => Number)
|
@Field(() => Decimal)
|
||||||
amount: number
|
amount: Decimal
|
||||||
|
|
||||||
@Field(() => Number)
|
@Field(() => Number)
|
||||||
moderator: number
|
moderator: number
|
||||||
|
|
||||||
@Field(() => [Number])
|
@Field(() => [Decimal])
|
||||||
creation: number[]
|
creation: Decimal[]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
import { User } from '@entity/User'
|
|
||||||
import { ObjectType, Field, Int } from 'type-graphql'
|
import { ObjectType, Field, Int } from 'type-graphql'
|
||||||
|
import Decimal from 'decimal.js-light'
|
||||||
|
import { User } from '@entity/User'
|
||||||
|
|
||||||
@ObjectType()
|
@ObjectType()
|
||||||
export class UserAdmin {
|
export class UserAdmin {
|
||||||
constructor(user: User, creation: number[], hasElopage: boolean, emailConfirmationSend: string) {
|
constructor(user: User, creation: Decimal[], hasElopage: boolean, emailConfirmationSend: string) {
|
||||||
this.userId = user.id
|
this.userId = user.id
|
||||||
this.email = user.email
|
this.email = user.email
|
||||||
this.firstName = user.firstName
|
this.firstName = user.firstName
|
||||||
@ -27,8 +28,8 @@ export class UserAdmin {
|
|||||||
@Field(() => String)
|
@Field(() => String)
|
||||||
lastName: string
|
lastName: string
|
||||||
|
|
||||||
@Field(() => [Number])
|
@Field(() => [Decimal])
|
||||||
creation: number[]
|
creation: Decimal[]
|
||||||
|
|
||||||
@Field(() => Boolean)
|
@Field(() => Boolean)
|
||||||
emailChecked: boolean
|
emailChecked: boolean
|
||||||
|
|||||||
@ -43,7 +43,7 @@ import CONFIG from '@/config'
|
|||||||
|
|
||||||
// const EMAIL_OPT_IN_REGISTER = 1
|
// const EMAIL_OPT_IN_REGISTER = 1
|
||||||
// const EMAIL_OPT_UNKNOWN = 3 // elopage?
|
// const EMAIL_OPT_UNKNOWN = 3 // elopage?
|
||||||
const MAX_CREATION_AMOUNT = 1000
|
const MAX_CREATION_AMOUNT = new Decimal(1000)
|
||||||
const FULL_CREATION_AVAILABLE = [MAX_CREATION_AMOUNT, MAX_CREATION_AMOUNT, MAX_CREATION_AMOUNT]
|
const FULL_CREATION_AVAILABLE = [MAX_CREATION_AMOUNT, MAX_CREATION_AMOUNT, MAX_CREATION_AMOUNT]
|
||||||
|
|
||||||
@Resolver()
|
@Resolver()
|
||||||
@ -170,7 +170,7 @@ export class AdminResolver {
|
|||||||
@Mutation(() => [Number])
|
@Mutation(() => [Number])
|
||||||
async createPendingCreation(
|
async createPendingCreation(
|
||||||
@Args() { email, amount, memo, creationDate, moderator }: CreatePendingCreationArgs,
|
@Args() { email, amount, memo, creationDate, moderator }: CreatePendingCreationArgs,
|
||||||
): Promise<number[]> {
|
): Promise<Decimal[]> {
|
||||||
const user = await dbUser.findOne({ email }, { withDeleted: true })
|
const user = await dbUser.findOne({ email }, { withDeleted: true })
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new Error(`Could not find user with email: ${email}`)
|
throw new Error(`Could not find user with email: ${email}`)
|
||||||
@ -186,7 +186,7 @@ export class AdminResolver {
|
|||||||
if (isCreationValid(creations, amount, creationDateObj)) {
|
if (isCreationValid(creations, amount, creationDateObj)) {
|
||||||
const adminPendingCreation = AdminPendingCreation.create()
|
const adminPendingCreation = AdminPendingCreation.create()
|
||||||
adminPendingCreation.userId = user.id
|
adminPendingCreation.userId = user.id
|
||||||
adminPendingCreation.amount = BigInt(amount)
|
adminPendingCreation.amount = amount
|
||||||
adminPendingCreation.created = new Date()
|
adminPendingCreation.created = new Date()
|
||||||
adminPendingCreation.date = creationDateObj
|
adminPendingCreation.date = creationDateObj
|
||||||
adminPendingCreation.memo = memo
|
adminPendingCreation.memo = memo
|
||||||
@ -251,14 +251,14 @@ export class AdminResolver {
|
|||||||
if (!isCreationValid(creations, amount, creationDateObj)) {
|
if (!isCreationValid(creations, amount, creationDateObj)) {
|
||||||
throw new Error('Creation is not valid')
|
throw new Error('Creation is not valid')
|
||||||
}
|
}
|
||||||
pendingCreationToUpdate.amount = BigInt(amount)
|
pendingCreationToUpdate.amount = amount
|
||||||
pendingCreationToUpdate.memo = memo
|
pendingCreationToUpdate.memo = memo
|
||||||
pendingCreationToUpdate.date = new Date(creationDate)
|
pendingCreationToUpdate.date = new Date(creationDate)
|
||||||
pendingCreationToUpdate.moderator = moderator
|
pendingCreationToUpdate.moderator = moderator
|
||||||
|
|
||||||
await AdminPendingCreation.save(pendingCreationToUpdate)
|
await AdminPendingCreation.save(pendingCreationToUpdate)
|
||||||
const result = new UpdatePendingCreation()
|
const result = new UpdatePendingCreation()
|
||||||
result.amount = parseInt(amount.toString())
|
result.amount = amount
|
||||||
result.memo = pendingCreationToUpdate.memo
|
result.memo = pendingCreationToUpdate.memo
|
||||||
result.date = pendingCreationToUpdate.date
|
result.date = pendingCreationToUpdate.date
|
||||||
result.moderator = pendingCreationToUpdate.moderator
|
result.moderator = pendingCreationToUpdate.moderator
|
||||||
@ -286,7 +286,7 @@ export class AdminResolver {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
...pendingCreation,
|
...pendingCreation,
|
||||||
amount: Number(pendingCreation.amount.toString()),
|
amount: pendingCreation.amount,
|
||||||
firstName: user ? user.firstName : '',
|
firstName: user ? user.firstName : '',
|
||||||
lastName: user ? user.lastName : '',
|
lastName: user ? user.lastName : '',
|
||||||
email: user ? user.email : '',
|
email: user ? user.email : '',
|
||||||
@ -318,7 +318,7 @@ export class AdminResolver {
|
|||||||
if (user.deletedAt) throw new Error('This user was deleted. Cannot confirm a creation.')
|
if (user.deletedAt) throw new Error('This user was deleted. Cannot confirm a creation.')
|
||||||
|
|
||||||
const creations = await getUserCreation(pendingCreation.userId, false)
|
const creations = await getUserCreation(pendingCreation.userId, false)
|
||||||
if (!isCreationValid(creations, Number(pendingCreation.amount), pendingCreation.date)) {
|
if (!isCreationValid(creations, pendingCreation.amount, pendingCreation.date)) {
|
||||||
throw new Error('Creation is not valid!!')
|
throw new Error('Creation is not valid!!')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -448,10 +448,10 @@ export class AdminResolver {
|
|||||||
|
|
||||||
interface CreationMap {
|
interface CreationMap {
|
||||||
id: number
|
id: number
|
||||||
creations: number[]
|
creations: Decimal[]
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getUserCreation(id: number, includePending = true): Promise<number[]> {
|
async function getUserCreation(id: number, includePending = true): Promise<Decimal[]> {
|
||||||
const creations = await getUserCreations([id], includePending)
|
const creations = await getUserCreations([id], includePending)
|
||||||
return creations[0] ? creations[0].creations : FULL_CREATION_AVAILABLE
|
return creations[0] ? creations[0].creations : FULL_CREATION_AVAILABLE
|
||||||
}
|
}
|
||||||
@ -493,30 +493,30 @@ async function getUserCreations(ids: number[], includePending = true): Promise<C
|
|||||||
(raw: { month: string; id: string; creation: number[] }) =>
|
(raw: { month: string; id: string; creation: number[] }) =>
|
||||||
parseInt(raw.month) === month && parseInt(raw.id) === id,
|
parseInt(raw.month) === month && parseInt(raw.id) === id,
|
||||||
)
|
)
|
||||||
return MAX_CREATION_AMOUNT - (creation ? Number(creation.sum) : 0)
|
return MAX_CREATION_AMOUNT.minus(creation ? creation.sum : 0)
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateCreations(creations: number[], pendingCreation: AdminPendingCreation): number[] {
|
function updateCreations(creations: Decimal[], pendingCreation: AdminPendingCreation): Decimal[] {
|
||||||
const index = getCreationIndex(pendingCreation.date.getMonth())
|
const index = getCreationIndex(pendingCreation.date.getMonth())
|
||||||
|
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
throw new Error('You cannot create GDD for a month older than the last three months.')
|
throw new Error('You cannot create GDD for a month older than the last three months.')
|
||||||
}
|
}
|
||||||
creations[index] += parseInt(pendingCreation.amount.toString())
|
creations[index] = creations[index].plus(pendingCreation.amount)
|
||||||
return creations
|
return creations
|
||||||
}
|
}
|
||||||
|
|
||||||
function isCreationValid(creations: number[], amount: number, creationDate: Date) {
|
function isCreationValid(creations: Decimal[], amount: Decimal, creationDate: Date) {
|
||||||
const index = getCreationIndex(creationDate.getMonth())
|
const index = getCreationIndex(creationDate.getMonth())
|
||||||
|
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
throw new Error(`No Creation found!`)
|
throw new Error(`No Creation found!`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (amount > creations[index]) {
|
if (amount.greaterThan(creations[index])) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`The amount (${amount} GDD) to be created exceeds the available amount (${creations[index]} GDD) for this month.`,
|
`The amount (${amount} GDD) to be created exceeds the available amount (${creations[index]} GDD) for this month.`,
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user