diff --git a/backend/src/graphql/enum/TypeId.ts b/backend/src/graphql/enum/TransactionTypeId.ts similarity index 66% rename from backend/src/graphql/enum/TypeId.ts rename to backend/src/graphql/enum/TransactionTypeId.ts index 29916331c..497ad5055 100644 --- a/backend/src/graphql/enum/TypeId.ts +++ b/backend/src/graphql/enum/TransactionTypeId.ts @@ -1,6 +1,6 @@ import { registerEnumType } from 'type-graphql' -export enum TypeId { +export enum TransactionTypeId { CREATION = 1, SEND = 2, RECEIVE = 3, @@ -8,7 +8,7 @@ export enum TypeId { DECAY = 4, } -registerEnumType(TypeId, { - name: 'TypeId', // this one is mandatory +registerEnumType(TransactionTypeId, { + name: 'TransactionTypeId', // this one is mandatory description: 'Type of the transaction', // this one is optional }) diff --git a/backend/src/graphql/resolver/AdminResolver.ts b/backend/src/graphql/resolver/AdminResolver.ts index 39e6dab3c..4ea1f2fd1 100644 --- a/backend/src/graphql/resolver/AdminResolver.ts +++ b/backend/src/graphql/resolver/AdminResolver.ts @@ -28,9 +28,12 @@ import { LoginEmailOptIn } from '@entity/LoginEmailOptIn' import { User } from '@entity/User' import { TransactionTypeId } from '../enum/TransactionTypeId' import Decimal from 'decimal.js-light' +import { Decay } from '../model/Decay' // const EMAIL_OPT_IN_REGISTER = 1 // const EMAIL_OPT_UNKNOWN = 3 // elopage? +const MAX_CREATION_AMOUNT = 1000 +const FULL_CREATION_AVAILABLE = [MAX_CREATION_AMOUNT, MAX_CREATION_AMOUNT, MAX_CREATION_AMOUNT] @Resolver() export class AdminResolver { @@ -104,7 +107,7 @@ export class AdminResolver { const userCreations = creations.find((c) => c.id === user.id) const adminUser = new UserAdmin( user, - userCreations ? userCreations.creations : [1000, 1000, 1000], + userCreations ? userCreations.creations : FULL_CREATION_AVAILABLE, await hasElopageBuys(user.email), emailConfirmationSend, ) @@ -270,11 +273,11 @@ export class AdminResolver { return { ...pendingCreation, - amount: Number(parseInt(pendingCreation.amount.toString()) / 10000), + amount: Number(pendingCreation.amount.toString()), firstName: user ? user.firstName : '', lastName: user ? user.lastName : '', email: user ? user.email : '', - creation: creation ? creation.creations : [1000, 1000, 1000], + creation: creation ? creation.creations : FULL_CREATION_AVAILABLE, } }) } @@ -300,7 +303,7 @@ export class AdminResolver { if (user.deletedAt) throw new Error('This user was deleted. Cannot confirm a creation.') const creations = await getUserCreation(pendingCreation.userId, false) - if (!isCreationValid(creations, Number(pendingCreation.amount) / 10000, pendingCreation.date)) { + if (!isCreationValid(creations, Number(pendingCreation.amount), pendingCreation.date)) { throw new Error('Creation is not valid!!') } @@ -310,25 +313,26 @@ export class AdminResolver { const lastTransaction = await transactionRepository.findLastForUser(pendingCreation.userId) let newBalance = new Decimal(0) + let decay: Decay | null = null if (lastTransaction) { - newBalance = calculateDecay( - lastTransaction.balance, - lastTransaction.balanceDate, - receivedCallDate, - ).balance + decay = calculateDecay(lastTransaction.balance, lastTransaction.balanceDate, receivedCallDate) + newBalance = decay.balance } // TODO pending creations decimal - newBalance = newBalance.add(new Decimal(Number(pendingCreation.amount))) + newBalance = newBalance.add(new Decimal(Number(pendingCreation.amount)).toString()) const transaction = new Transaction() transaction.typeId = TransactionTypeId.CREATION transaction.memo = pendingCreation.memo transaction.userId = pendingCreation.userId + transaction.previous = lastTransaction ? lastTransaction.id : null // TODO pending creations decimal transaction.amount = new Decimal(Number(pendingCreation.amount)) transaction.creationDate = pendingCreation.date transaction.balance = newBalance transaction.balanceDate = receivedCallDate + transaction.decay = decay ? decay.decay : new Decimal(0) + transaction.decayStart = decay ? decay.start : null await transaction.save() await AdminPendingCreation.delete(pendingCreation) @@ -344,7 +348,7 @@ interface CreationMap { async function getUserCreation(id: number, includePending = true): Promise { const creations = await getUserCreations([id], includePending) - return creations[0] ? creations[0].creations : [1000, 1000, 1000] + return creations[0] ? creations[0].creations : FULL_CREATION_AVAILABLE } async function getUserCreations(ids: number[], includePending = true): Promise { @@ -384,7 +388,7 @@ async function getUserCreations(ids: number[], includePending = true): Promise parseInt(raw.month) === month && parseInt(raw.id) === id, ) - return 1000 - (creation ? Number(creation.sum) / 10000 : 0) + return MAX_CREATION_AMOUNT - (creation ? Number(creation.sum) : 0) }), } }) diff --git a/database/entity/0029-clean_transaction_table/Transaction.ts b/database/entity/0029-clean_transaction_table/Transaction.ts index 7989e918a..1212d62ce 100644 --- a/database/entity/0029-clean_transaction_table/Transaction.ts +++ b/database/entity/0029-clean_transaction_table/Transaction.ts @@ -11,7 +11,7 @@ export class Transaction extends BaseEntity { userId: number @Column({ unsigned: true, nullable: true, default: null }) - previous: number + previous: number | null @Column({ name: 'type_id', unsigned: true, nullable: false }) typeId: number