mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
add openCreation graphqlModel and resolver
This commit is contained in:
parent
685c4e947f
commit
dc33427cd0
@ -35,6 +35,7 @@ export enum RIGHTS {
|
||||
SEARCH_ADMIN_USERS = 'SEARCH_ADMIN_USERS',
|
||||
CREATE_CONTRIBUTION_MESSAGE = 'CREATE_CONTRIBUTION_MESSAGE',
|
||||
LIST_ALL_CONTRIBUTION_MESSAGES = 'LIST_ALL_CONTRIBUTION_MESSAGES',
|
||||
OPEN_CREATIONS = 'OPEN_CREATIONS',
|
||||
// Admin
|
||||
SEARCH_USERS = 'SEARCH_USERS',
|
||||
SET_USER_ROLE = 'SET_USER_ROLE',
|
||||
|
||||
@ -33,6 +33,7 @@ export const ROLE_USER = new Role('user', [
|
||||
RIGHTS.COMMUNITY_STATISTICS,
|
||||
RIGHTS.CREATE_CONTRIBUTION_MESSAGE,
|
||||
RIGHTS.LIST_ALL_CONTRIBUTION_MESSAGES,
|
||||
RIGHTS.OPEN_CREATIONS,
|
||||
])
|
||||
export const ROLE_ADMIN = new Role('admin', Object.values(RIGHTS)) // all rights
|
||||
|
||||
|
||||
14
backend/src/graphql/model/OpenCreation.ts
Normal file
14
backend/src/graphql/model/OpenCreation.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { ObjectType, Field, Int } from 'type-graphql'
|
||||
import Decimal from 'decimal.js-light'
|
||||
|
||||
@ObjectType()
|
||||
export class OpenCreation {
|
||||
@Field(() => Int)
|
||||
month: number
|
||||
|
||||
@Field(() => Int)
|
||||
year: number
|
||||
|
||||
@Field(() => Decimal)
|
||||
amount: Decimal
|
||||
}
|
||||
@ -11,8 +11,9 @@ import { Transaction as DbTransaction } from '@entity/Transaction'
|
||||
import { AdminCreateContributions } from '@model/AdminCreateContributions'
|
||||
import { AdminUpdateContribution } from '@model/AdminUpdateContribution'
|
||||
import { Contribution, ContributionListResult } from '@model/Contribution'
|
||||
import { UnconfirmedContribution } from '@model/UnconfirmedContribution'
|
||||
import { Decay } from '@model/Decay'
|
||||
import { OpenCreation } from '@model/OpenCreation'
|
||||
import { UnconfirmedContribution } from '@model/UnconfirmedContribution'
|
||||
import { TransactionTypeId } from '@enum/TransactionTypeId'
|
||||
import { Order } from '@enum/Order'
|
||||
import { ContributionType } from '@enum/ContributionType'
|
||||
@ -27,6 +28,7 @@ import { RIGHTS } from '@/auth/RIGHTS'
|
||||
import { Context, getUser, getClientTimezoneOffset } from '@/server/context'
|
||||
import { backendLogger as logger } from '@/server/logger'
|
||||
import {
|
||||
getCreationDates,
|
||||
getUserCreation,
|
||||
getUserCreations,
|
||||
validateContribution,
|
||||
@ -683,4 +685,23 @@ export class ContributionResolver {
|
||||
)
|
||||
// return userTransactions.map((t) => new Transaction(t, new User(user), communityUser))
|
||||
}
|
||||
|
||||
@Authorized([RIGHTS.OPEN_CREATIONS])
|
||||
@Query(() => [OpenCreation])
|
||||
async openCreations(
|
||||
@Arg('userId', () => Int, { nullable: true }) userId: number | null,
|
||||
@Ctx() context: Context,
|
||||
): Promise<OpenCreation[]> {
|
||||
const id = userId || getUser(context).id
|
||||
const clientTimezoneOffset = getClientTimezoneOffset(context)
|
||||
const creationDates = getCreationDates(clientTimezoneOffset)
|
||||
const creations = await getUserCreation(id, clientTimezoneOffset)
|
||||
return creationDates.map((date, index) => {
|
||||
return {
|
||||
month: date.getMonth(),
|
||||
year: date.getFullYear(),
|
||||
amount: creations[index],
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,15 +101,19 @@ export const getUserCreation = async (
|
||||
}
|
||||
|
||||
const getCreationMonths = (timezoneOffset: number): number[] => {
|
||||
return getCreationDates(timezoneOffset).map((date) => date.getMonth() + 1)
|
||||
}
|
||||
|
||||
export const getCreationDates = (timezoneOffset: number): Date[] => {
|
||||
const clientNow = new Date()
|
||||
clientNow.setTime(clientNow.getTime() - timezoneOffset * 60 * 1000)
|
||||
logger.info(
|
||||
`getCreationMonths -- offset: ${timezoneOffset} -- clientNow: ${clientNow.toISOString()}`,
|
||||
)
|
||||
return [
|
||||
new Date(clientNow.getFullYear(), clientNow.getMonth() - 2, 1).getMonth() + 1,
|
||||
new Date(clientNow.getFullYear(), clientNow.getMonth() - 1, 1).getMonth() + 1,
|
||||
clientNow.getMonth() + 1,
|
||||
new Date(clientNow.getFullYear(), clientNow.getMonth() - 2, 1),
|
||||
new Date(clientNow.getFullYear(), clientNow.getMonth() - 1, 1),
|
||||
clientNow,
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user