mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
141 lines
6.9 KiB
TypeScript
141 lines
6.9 KiB
TypeScript
import { Resolver, Query, Arg, Args } from 'type-graphql'
|
|
import { getCustomRepository, Raw } from 'typeorm'
|
|
import { UserAdmin } from '../model/UserAdmin'
|
|
import { LoginUserRepository } from '../../typeorm/repository/LoginUser'
|
|
import { TransactionCreationRepository } from '../../typeorm/repository/TransactionCreation'
|
|
import { PendingCreationRepository } from '../../typeorm/repository/PendingCreation'
|
|
import { UserRepository } from '../../typeorm/repository/User'
|
|
import CreatePendingCreationArgs from '../arg/CreatePendingCreationArgs'
|
|
|
|
@Resolver()
|
|
export class AdminResolver {
|
|
@Query(() => [UserAdmin])
|
|
async searchUsers(@Arg('searchText') searchText: string): Promise<UserAdmin[]> {
|
|
const loginUserRepository = getCustomRepository(LoginUserRepository)
|
|
const loginUsers = await loginUserRepository.findBySearchCriteria(searchText)
|
|
const users = loginUsers.map((loginUser) => {
|
|
const user = new UserAdmin()
|
|
user.firstName = loginUser.firstName
|
|
user.lastName = loginUser.lastName
|
|
user.email = loginUser.email
|
|
user.creation = [10000000, 10000000, 10000000] // await getUserCreations(loginUser.id)
|
|
return user
|
|
})
|
|
return users
|
|
}
|
|
|
|
@Query(() => Boolean)
|
|
async createPendingCreation(
|
|
@Args() { email, amount, note, creationDate }: CreatePendingCreationArgs,
|
|
): Promise<boolean> {
|
|
// TODO: Check user validity
|
|
const userRepository = getCustomRepository(UserRepository)
|
|
const user = await userRepository.findByEmail(email)
|
|
// TODO: Check user open creation state (Open creation)
|
|
const creations = await getUserCreations(user.id)
|
|
console.log('creations', creations)
|
|
if (isCreationValid(creations, amount, creationDate)) {
|
|
// UserAdmin.creations()
|
|
// TODO: Write pending creation to DB
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
async function getUserCreations(id: number): Promise<number[]> {
|
|
// TODO: NOW()-ActualDays - 2 Monate
|
|
// const transactionCreations = await getCustomRepository(TransactionCreationRepository).find({
|
|
// userId: id,
|
|
// targetDate: Raw((alias) => `${alias} > :date`, { date: "2021-09-01" /* TODO: NOW().format("YYYY-MM") + '-01' - 2 Month */ }),
|
|
// })
|
|
const transactionCreationRepository = getCustomRepository(TransactionCreationRepository)
|
|
const createdAmountBeforeLastMonth = await transactionCreationRepository
|
|
.createQueryBuilder('transaction_creations')
|
|
.select('SUM(transaction_creations.amount)', 'sum')
|
|
.where('transaction_creations.state_user_id = :id', { id })
|
|
.andWhere({
|
|
targetDate: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { date: "2021-09-01", enddate: "2021-10-01" /* TODO: NOW().format("YYYY-MM") + '-01' - 2 Month */ })
|
|
})
|
|
.getRawOne()
|
|
console.log('createdAmountBeforeLastMonth.sum', Number(createdAmountBeforeLastMonth.sum))
|
|
|
|
const createdAmountLastMonth = await transactionCreationRepository
|
|
.createQueryBuilder('transaction_creations')
|
|
.select('SUM(transaction_creations.amount)', 'sum')
|
|
.where('transaction_creations.state_user_id = :id', { id })
|
|
.andWhere({
|
|
targetDate: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { date: "2021-10-01", enddate: "2021-11-01" /* TODO: NOW().format("YYYY-MM") + '-01' - 2 Month */ })
|
|
})
|
|
.getRawOne()
|
|
console.log('createdAmountLastMonth.sum', Number(createdAmountLastMonth.sum))
|
|
|
|
const createdAmountMonth = await transactionCreationRepository
|
|
.createQueryBuilder('transaction_creations')
|
|
.select('SUM(transaction_creations.amount)', 'sum')
|
|
.where('transaction_creations.state_user_id = :id', { id })
|
|
.andWhere({
|
|
targetDate: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { date: "2021-11-01", enddate: "2021-12-01" /* TODO: NOW().format("YYYY-MM") + '-01' - 2 Month */ })
|
|
})
|
|
.getRawOne()
|
|
console.log('createdAmountMonth.sum', Number(createdAmountMonth.sum))
|
|
|
|
// const transactionCreationsMonthQuery = await findAllUserTransactionCreations.andWhere({
|
|
// targetDate: Raw((alias) => `${alias} > :date and ${alias} < :enddate`, { date: "2021-11-01", enddate: "2021-12-01" /* TODO: NOW().format("YYYY-MM") + '-01' - 2 Month */ })
|
|
// })
|
|
// const createdAmountMonth = await transactionCreationsMonthQuery.getRawOne()
|
|
// console.log('createdAmountMonth', createdAmountMonth)
|
|
// const transactionCreationsLastThreeMonth = await transactionCreationsQuery.getRawOne()
|
|
// console.log('transactionCreations', transactionCreations)
|
|
// SELECT * FROM pending_creations WHERE userId = id
|
|
const pendingCreationRepository = getCustomRepository(PendingCreationRepository)
|
|
const pendingAmountMounth = await pendingCreationRepository.createQueryBuilder('login_pending_tasks_admin')
|
|
.select('SUM(login_pending_tasks_admin.amount)', 'sum')
|
|
.where('login_pending_tasks_admin.userId = :id', { id })
|
|
.andWhere({
|
|
date: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { date: "2021-11-01", enddate: "2021-12-01" /* TODO: NOW().format("YYYY-MM") + '-01' - 2 Month */ })
|
|
})
|
|
.getRawOne()
|
|
console.log('pendingAmountMounth', Number(pendingAmountMounth.sum))
|
|
|
|
const pendingAmountLastMounth = await pendingCreationRepository.createQueryBuilder('login_pending_tasks_admin')
|
|
.select('SUM(login_pending_tasks_admin.amount)', 'sum')
|
|
.where('login_pending_tasks_admin.userId = :id', { id })
|
|
.andWhere({
|
|
date: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { date: "2021-10-01", enddate: "2021-11-01" /* TODO: NOW().format("YYYY-MM") + '-01' - 2 Month */ })
|
|
})
|
|
.getRawOne()
|
|
console.log('pendingAmountLastMounth', Number(pendingAmountLastMounth.sum))
|
|
|
|
const pendingAmountBeforeLastMounth = await pendingCreationRepository.createQueryBuilder('login_pending_tasks_admin')
|
|
.select('SUM(login_pending_tasks_admin.amount)', 'sum')
|
|
.where('login_pending_tasks_admin.userId = :id', { id })
|
|
.andWhere({
|
|
date: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { date: "2021-09-01", enddate: "2021-10-01" /* TODO: NOW().format("YYYY-MM") + '-01' - 2 Month */ })
|
|
})
|
|
.getRawOne()
|
|
console.log('pendingAmountBeforeLastMounth', Number(pendingAmountBeforeLastMounth.sum))
|
|
// const pendingCreations = await getCustomRepository(PendingCreationRepository).find({
|
|
// userId: id,
|
|
// date: Raw((alias) => `${alias} > :date`, { date: "2021-09-01" /* TODO: NOW().format("YYYY-MM") + '-01' - 2 Month */ }),
|
|
// })
|
|
|
|
|
|
// const createdAmountLastMonth = ...
|
|
// const createdAmountCurrentMonth = ...
|
|
|
|
// COUNT amount from 2 tables
|
|
// if amount < 3000 => Store in pending_creations
|
|
const usedCreationBeforeLastMonth = Number(createdAmountBeforeLastMonth.sum) + Number()
|
|
const usedCreationLastMonth = Number(createdAmountLastMonth.sum) + Number()
|
|
const usedCreationMonth = Number(createdAmountMonth.sum) + Number()
|
|
return [
|
|
10000000 - usedCreationBeforeLastMonth,
|
|
10000000 - usedCreationLastMonth,
|
|
10000000 - usedCreationMonth,
|
|
]
|
|
}
|
|
|
|
function isCreationValid(creations: number[], amount: any, creationDate: any) {
|
|
return true
|
|
}
|