renamed TypeId to TransactiontypeId

fixed admin creation (backend part)
This commit is contained in:
Ulf Gebhardt 2022-03-04 15:12:51 +01:00
parent ae4ad937aa
commit 22c65aa929
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
3 changed files with 20 additions and 16 deletions

View File

@ -1,6 +1,6 @@
import { registerEnumType } from 'type-graphql' import { registerEnumType } from 'type-graphql'
export enum TypeId { export enum TransactionTypeId {
CREATION = 1, CREATION = 1,
SEND = 2, SEND = 2,
RECEIVE = 3, RECEIVE = 3,
@ -8,7 +8,7 @@ export enum TypeId {
DECAY = 4, DECAY = 4,
} }
registerEnumType(TypeId, { registerEnumType(TransactionTypeId, {
name: 'TypeId', // this one is mandatory name: 'TransactionTypeId', // this one is mandatory
description: 'Type of the transaction', // this one is optional description: 'Type of the transaction', // this one is optional
}) })

View File

@ -28,9 +28,12 @@ import { LoginEmailOptIn } from '@entity/LoginEmailOptIn'
import { User } from '@entity/User' import { User } from '@entity/User'
import { TransactionTypeId } from '../enum/TransactionTypeId' import { TransactionTypeId } from '../enum/TransactionTypeId'
import Decimal from 'decimal.js-light' import Decimal from 'decimal.js-light'
import { Decay } from '../model/Decay'
// 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 FULL_CREATION_AVAILABLE = [MAX_CREATION_AMOUNT, MAX_CREATION_AMOUNT, MAX_CREATION_AMOUNT]
@Resolver() @Resolver()
export class AdminResolver { export class AdminResolver {
@ -104,7 +107,7 @@ export class AdminResolver {
const userCreations = creations.find((c) => c.id === user.id) const userCreations = creations.find((c) => c.id === user.id)
const adminUser = new UserAdmin( const adminUser = new UserAdmin(
user, user,
userCreations ? userCreations.creations : [1000, 1000, 1000], userCreations ? userCreations.creations : FULL_CREATION_AVAILABLE,
await hasElopageBuys(user.email), await hasElopageBuys(user.email),
emailConfirmationSend, emailConfirmationSend,
) )
@ -270,11 +273,11 @@ export class AdminResolver {
return { return {
...pendingCreation, ...pendingCreation,
amount: Number(parseInt(pendingCreation.amount.toString()) / 10000), amount: Number(pendingCreation.amount.toString()),
firstName: user ? user.firstName : '', firstName: user ? user.firstName : '',
lastName: user ? user.lastName : '', lastName: user ? user.lastName : '',
email: user ? user.email : '', 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.') 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) / 10000, pendingCreation.date)) { if (!isCreationValid(creations, Number(pendingCreation.amount), pendingCreation.date)) {
throw new Error('Creation is not valid!!') throw new Error('Creation is not valid!!')
} }
@ -310,25 +313,26 @@ export class AdminResolver {
const lastTransaction = await transactionRepository.findLastForUser(pendingCreation.userId) const lastTransaction = await transactionRepository.findLastForUser(pendingCreation.userId)
let newBalance = new Decimal(0) let newBalance = new Decimal(0)
let decay: Decay | null = null
if (lastTransaction) { if (lastTransaction) {
newBalance = calculateDecay( decay = calculateDecay(lastTransaction.balance, lastTransaction.balanceDate, receivedCallDate)
lastTransaction.balance, newBalance = decay.balance
lastTransaction.balanceDate,
receivedCallDate,
).balance
} }
// TODO pending creations decimal // 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() const transaction = new Transaction()
transaction.typeId = TransactionTypeId.CREATION transaction.typeId = TransactionTypeId.CREATION
transaction.memo = pendingCreation.memo transaction.memo = pendingCreation.memo
transaction.userId = pendingCreation.userId transaction.userId = pendingCreation.userId
transaction.previous = lastTransaction ? lastTransaction.id : null
// TODO pending creations decimal // TODO pending creations decimal
transaction.amount = new Decimal(Number(pendingCreation.amount)) transaction.amount = new Decimal(Number(pendingCreation.amount))
transaction.creationDate = pendingCreation.date transaction.creationDate = pendingCreation.date
transaction.balance = newBalance transaction.balance = newBalance
transaction.balanceDate = receivedCallDate transaction.balanceDate = receivedCallDate
transaction.decay = decay ? decay.decay : new Decimal(0)
transaction.decayStart = decay ? decay.start : null
await transaction.save() await transaction.save()
await AdminPendingCreation.delete(pendingCreation) await AdminPendingCreation.delete(pendingCreation)
@ -344,7 +348,7 @@ interface CreationMap {
async function getUserCreation(id: number, includePending = true): Promise<number[]> { async function getUserCreation(id: number, includePending = true): Promise<number[]> {
const creations = await getUserCreations([id], includePending) 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<CreationMap[]> { async function getUserCreations(ids: number[], includePending = true): Promise<CreationMap[]> {
@ -384,7 +388,7 @@ 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 1000 - (creation ? Number(creation.sum) / 10000 : 0) return MAX_CREATION_AMOUNT - (creation ? Number(creation.sum) : 0)
}), }),
} }
}) })

View File

@ -11,7 +11,7 @@ export class Transaction extends BaseEntity {
userId: number userId: number
@Column({ unsigned: true, nullable: true, default: null }) @Column({ unsigned: true, nullable: true, default: null })
previous: number previous: number | null
@Column({ name: 'type_id', unsigned: true, nullable: false }) @Column({ name: 'type_id', unsigned: true, nullable: false })
typeId: number typeId: number