do not use moment anymore

This commit is contained in:
Moriz Wahl 2022-02-22 15:49:58 +01:00
parent 55211f0940
commit cd7a9bf361

View File

@ -12,7 +12,6 @@ import { UserRepository } from '../../typeorm/repository/User'
import CreatePendingCreationArgs from '../arg/CreatePendingCreationArgs' import CreatePendingCreationArgs from '../arg/CreatePendingCreationArgs'
import UpdatePendingCreationArgs from '../arg/UpdatePendingCreationArgs' import UpdatePendingCreationArgs from '../arg/UpdatePendingCreationArgs'
import SearchUsersArgs from '../arg/SearchUsersArgs' import SearchUsersArgs from '../arg/SearchUsersArgs'
import moment from 'moment'
import { Transaction } from '@entity/Transaction' import { Transaction } from '@entity/Transaction'
import { UserTransaction } from '@entity/UserTransaction' import { UserTransaction } from '@entity/UserTransaction'
import { UserTransactionRepository } from '../../typeorm/repository/UserTransaction' import { UserTransactionRepository } from '../../typeorm/repository/UserTransaction'
@ -301,12 +300,7 @@ async function getUserCreation(id: number): Promise<number[]> {
} }
async function getUserCreations(ids: number[]): Promise<CreationMap[]> { async function getUserCreations(ids: number[]): Promise<CreationMap[]> {
const now = new Date(Date.now()) const months = getCreationMonths()
const months = [
now.getMonth() + 1,
new Date(now.getFullYear(), now.getMonth() - 1, 1).getMonth() + 1,
new Date(now.getFullYear(), now.getMonth() - 2, 1).getMonth() + 1,
].reverse()
const queryRunner = getConnection().createQueryRunner() const queryRunner = getConnection().createQueryRunner()
await queryRunner.connect() await queryRunner.connect()
@ -344,50 +338,38 @@ async function getUserCreations(ids: number[]): Promise<CreationMap[]> {
} }
function updateCreations(creations: number[], pendingCreation: AdminPendingCreation): number[] { function updateCreations(creations: number[], pendingCreation: AdminPendingCreation): number[] {
const dateMonth = moment().format('YYYY-MM') const index = getCreationIndex(pendingCreation.date.getMonth())
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) { if (index > -1) {
case dateMonth: creations[index] += parseInt(pendingCreation.amount.toString())
creations[2] += parseInt(pendingCreation.amount.toString()) } else {
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') throw new Error('UpdatedCreationDate is not in the last three months')
} }
return creations 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 index = getCreationIndex(creationDate.getMonth())
const dateLastMonth = moment().subtract(1, 'month').format('YYYY-MM')
const dateBeforeLastMonth = moment().subtract(2, 'month').format('YYYY-MM')
const creationDateMonth = moment(creationDate).format('YYYY-MM')
let openCreation if (index > -1) {
switch (creationDateMonth) { if (creations[index] < amount) {
case dateMonth: throw new Error(`Open creation (${creations[index]}) is less than amount (${amount})`)
openCreation = creations[2] } else {
break
case dateLastMonth:
openCreation = creations[1]
break
case dateBeforeLastMonth:
openCreation = creations[0]
break
default:
throw new Error('CreationDate is not in last three months')
}
if (openCreation < amount) {
throw new Error(`Open creation (${openCreation}) is less than amount (${amount})`)
}
return true return true
} }
}
}
const getCreationMonths = (): number[] => {
const now = new Date(Date.now())
return [
now.getMonth() + 1,
new Date(now.getFullYear(), now.getMonth() - 1, 1).getMonth() + 1,
new Date(now.getFullYear(), now.getMonth() - 2, 1).getMonth() + 1,
].reverse()
}
const getCreationIndex = (month: number): number => {
return getCreationMonths().findIndex((el) => el === month + 1)
}