mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Merge pull request #1346 from gradido/1330-EditCreationFormular-update-date
fix updatePendingCreation method
This commit is contained in:
commit
0ed79cdb54
@ -23,6 +23,7 @@ import { UserTransactionRepository } from '../../typeorm/repository/UserTransact
|
|||||||
import { BalanceRepository } from '../../typeorm/repository/Balance'
|
import { BalanceRepository } from '../../typeorm/repository/Balance'
|
||||||
import { calculateDecay } from '../../util/decay'
|
import { calculateDecay } from '../../util/decay'
|
||||||
import { LoginUserRepository } from '../../typeorm/repository/LoginUser'
|
import { LoginUserRepository } from '../../typeorm/repository/LoginUser'
|
||||||
|
import { LoginPendingTasksAdmin } from '@entity/LoginPendingTasksAdmin'
|
||||||
|
|
||||||
@Resolver()
|
@Resolver()
|
||||||
export class AdminResolver {
|
export class AdminResolver {
|
||||||
@ -113,22 +114,32 @@ export class AdminResolver {
|
|||||||
const user = await userRepository.findByEmail(email)
|
const user = await userRepository.findByEmail(email)
|
||||||
|
|
||||||
const loginPendingTasksAdminRepository = getCustomRepository(LoginPendingTasksAdminRepository)
|
const loginPendingTasksAdminRepository = getCustomRepository(LoginPendingTasksAdminRepository)
|
||||||
const updatedCreation = await loginPendingTasksAdminRepository.findOneOrFail({ id })
|
const pendingCreationToUpdate = await loginPendingTasksAdminRepository.findOneOrFail({ id })
|
||||||
|
|
||||||
if (updatedCreation.userId !== user.id)
|
if (pendingCreationToUpdate.userId !== user.id) {
|
||||||
throw new Error('user of the pending creation and send user does not correspond')
|
throw new Error('user of the pending creation and send user does not correspond')
|
||||||
|
}
|
||||||
|
|
||||||
updatedCreation.amount = BigInt(amount * 10000)
|
const creationDateObj = new Date(creationDate)
|
||||||
updatedCreation.memo = memo
|
let creations = await getUserCreations(user.id)
|
||||||
updatedCreation.date = new Date(creationDate)
|
if (pendingCreationToUpdate.date.getMonth() === creationDateObj.getMonth()) {
|
||||||
updatedCreation.moderator = moderator
|
creations = updateCreations(creations, pendingCreationToUpdate)
|
||||||
|
}
|
||||||
|
|
||||||
await loginPendingTasksAdminRepository.save(updatedCreation)
|
if (!isCreationValid(creations, amount, creationDateObj)) {
|
||||||
|
throw new Error('Creation is not valid')
|
||||||
|
}
|
||||||
|
pendingCreationToUpdate.amount = BigInt(amount * 10000)
|
||||||
|
pendingCreationToUpdate.memo = memo
|
||||||
|
pendingCreationToUpdate.date = new Date(creationDate)
|
||||||
|
pendingCreationToUpdate.moderator = moderator
|
||||||
|
|
||||||
|
await loginPendingTasksAdminRepository.save(pendingCreationToUpdate)
|
||||||
const result = new UpdatePendingCreation()
|
const result = new UpdatePendingCreation()
|
||||||
result.amount = parseInt(amount.toString())
|
result.amount = parseInt(amount.toString())
|
||||||
result.memo = updatedCreation.memo
|
result.memo = pendingCreationToUpdate.memo
|
||||||
result.date = updatedCreation.date
|
result.date = pendingCreationToUpdate.date
|
||||||
result.moderator = updatedCreation.moderator
|
result.moderator = pendingCreationToUpdate.moderator
|
||||||
result.creation = await getUserCreations(user.id)
|
result.creation = await getUserCreations(user.id)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
@ -322,6 +333,28 @@ async function getUserCreations(id: number): Promise<number[]> {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateCreations(creations: number[], pendingCreation: LoginPendingTasksAdmin): number[] {
|
||||||
|
const dateMonth = moment().format('YYYY-MM')
|
||||||
|
const dateLastMonth = moment().subtract(1, 'month').format('YYYY-MM')
|
||||||
|
const dateBeforeLastMonth = moment().subtract(2, 'month').format('YYYY-MM')
|
||||||
|
const creationDateMonth = moment(pendingCreation.date).format('YYYY-MM')
|
||||||
|
|
||||||
|
switch (creationDateMonth) {
|
||||||
|
case dateMonth:
|
||||||
|
creations[2] += parseInt(pendingCreation.amount.toString())
|
||||||
|
break
|
||||||
|
case dateLastMonth:
|
||||||
|
creations[1] += parseInt(pendingCreation.amount.toString())
|
||||||
|
break
|
||||||
|
case dateBeforeLastMonth:
|
||||||
|
creations[0] += parseInt(pendingCreation.amount.toString())
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
throw new Error('UpdatedCreationDate is not in the last three months')
|
||||||
|
}
|
||||||
|
return creations
|
||||||
|
}
|
||||||
|
|
||||||
function isCreationValid(creations: number[], amount: number, creationDate: Date) {
|
function isCreationValid(creations: number[], amount: number, creationDate: Date) {
|
||||||
const dateMonth = moment().format('YYYY-MM')
|
const dateMonth = moment().format('YYYY-MM')
|
||||||
const dateLastMonth = moment().subtract(1, 'month').format('YYYY-MM')
|
const dateLastMonth = moment().subtract(1, 'month').format('YYYY-MM')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user