mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
combine logic for both listTransactionLinks and listTransactionLinksAdmin
This commit is contained in:
parent
9ff045fdbf
commit
66a5175dc1
@ -61,8 +61,8 @@ export class TransactionLink {
|
||||
@ObjectType()
|
||||
export class TransactionLinkResult {
|
||||
@Field(() => Int)
|
||||
linkCount: number
|
||||
count: number
|
||||
|
||||
@Field(() => [TransactionLink])
|
||||
linkList: TransactionLink[]
|
||||
links: TransactionLink[]
|
||||
}
|
||||
|
||||
@ -611,8 +611,8 @@ describe('TransactionLinkResolver', () => {
|
||||
expect.objectContaining({
|
||||
data: {
|
||||
listTransactionLinksAdmin: {
|
||||
linkCount: 6,
|
||||
linkList: expect.not.arrayContaining([
|
||||
count: 6,
|
||||
links: expect.not.arrayContaining([
|
||||
expect.objectContaining({
|
||||
memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(',
|
||||
createdAt: expect.any(String),
|
||||
@ -647,8 +647,8 @@ describe('TransactionLinkResolver', () => {
|
||||
expect.objectContaining({
|
||||
data: {
|
||||
listTransactionLinksAdmin: {
|
||||
linkCount: 6,
|
||||
linkList: expect.not.arrayContaining([
|
||||
count: 6,
|
||||
links: expect.not.arrayContaining([
|
||||
expect.objectContaining({
|
||||
memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(',
|
||||
createdAt: expect.any(String),
|
||||
@ -681,8 +681,8 @@ describe('TransactionLinkResolver', () => {
|
||||
expect.objectContaining({
|
||||
data: {
|
||||
listTransactionLinksAdmin: {
|
||||
linkCount: 7,
|
||||
linkList: expect.arrayContaining([
|
||||
count: 7,
|
||||
links: expect.arrayContaining([
|
||||
expect.not.objectContaining({
|
||||
memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(',
|
||||
createdAt: expect.any(String),
|
||||
@ -715,8 +715,8 @@ describe('TransactionLinkResolver', () => {
|
||||
expect.objectContaining({
|
||||
data: {
|
||||
listTransactionLinksAdmin: {
|
||||
linkCount: 7,
|
||||
linkList: expect.arrayContaining([
|
||||
count: 7,
|
||||
links: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(',
|
||||
createdAt: expect.any(String),
|
||||
@ -752,8 +752,8 @@ describe('TransactionLinkResolver', () => {
|
||||
expect.objectContaining({
|
||||
data: {
|
||||
listTransactionLinksAdmin: {
|
||||
linkCount: 6,
|
||||
linkList: expect.arrayContaining([
|
||||
count: 6,
|
||||
links: expect.arrayContaining([
|
||||
expect.not.objectContaining({
|
||||
memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(',
|
||||
createdAt: expect.any(String),
|
||||
|
||||
@ -35,6 +35,7 @@ import { TRANSACTIONS_LOCK } from '@/util/TRANSACTIONS_LOCK'
|
||||
import LogError from '@/server/LogError'
|
||||
|
||||
import { getLastTransaction } from './util/getLastTransaction'
|
||||
import { filter } from 'lodash'
|
||||
|
||||
// TODO: do not export, test it inside the resolver
|
||||
export const transactionLinkCode = (date: Date): string => {
|
||||
@ -141,30 +142,6 @@ export class TransactionLinkResolver {
|
||||
}
|
||||
}
|
||||
|
||||
@Authorized([RIGHTS.LIST_TRANSACTION_LINKS])
|
||||
@Query(() => [TransactionLink])
|
||||
async listTransactionLinks(
|
||||
@Args()
|
||||
{ currentPage = 1, pageSize = 5, order = Order.DESC }: Paginated,
|
||||
@Ctx() context: Context,
|
||||
): Promise<TransactionLink[]> {
|
||||
const user = getUser(context)
|
||||
// const now = new Date()
|
||||
const transactionLinks = await DbTransactionLink.find({
|
||||
where: {
|
||||
userId: user.id,
|
||||
redeemedBy: null,
|
||||
// validUntil: MoreThan(now),
|
||||
},
|
||||
order: {
|
||||
createdAt: order,
|
||||
},
|
||||
skip: (currentPage - 1) * pageSize,
|
||||
take: pageSize,
|
||||
})
|
||||
return transactionLinks.map((tl) => new TransactionLink(tl, new User(user)))
|
||||
}
|
||||
|
||||
@Authorized([RIGHTS.REDEEM_TRANSACTION_LINK])
|
||||
@Mutation(() => Boolean)
|
||||
async redeemTransactionLink(
|
||||
@ -338,43 +315,66 @@ export class TransactionLinkResolver {
|
||||
}
|
||||
}
|
||||
|
||||
@Authorized([RIGHTS.LIST_TRANSACTION_LINKS])
|
||||
@Query(() => [TransactionLink])
|
||||
async listTransactionLinks(
|
||||
@Args()
|
||||
paginated: Paginated,
|
||||
@Ctx() context: Context,
|
||||
): Promise<TransactionLinkResult> {
|
||||
const user = getUser(context)
|
||||
return transactionLinkList(
|
||||
paginated,
|
||||
{
|
||||
withDeleted: false,
|
||||
withExpired: true,
|
||||
withRedeemed: false,
|
||||
},
|
||||
user.id,
|
||||
)
|
||||
}
|
||||
|
||||
@Authorized([RIGHTS.LIST_TRANSACTION_LINKS_ADMIN])
|
||||
@Query(() => TransactionLinkResult)
|
||||
async listTransactionLinksAdmin(
|
||||
@Args()
|
||||
{ currentPage = 1, pageSize = 5, order = Order.DESC }: Paginated,
|
||||
paginated: Paginated,
|
||||
@Arg('filters', () => TransactionLinkFilters, { nullable: true })
|
||||
filters: TransactionLinkFilters,
|
||||
filters: TransactionLinkFilters | null,
|
||||
@Arg('userId', () => Int)
|
||||
userId: number,
|
||||
): Promise<TransactionLinkResult> {
|
||||
const user = await DbUser.findOneOrFail({ id: userId })
|
||||
const where: {
|
||||
userId: number
|
||||
redeemedBy?: number | null
|
||||
validUntil?: FindOperator<Date> | null
|
||||
} = {
|
||||
userId,
|
||||
redeemedBy: null,
|
||||
validUntil: MoreThan(new Date()),
|
||||
}
|
||||
if (filters) {
|
||||
if (filters.withRedeemed) delete where.redeemedBy
|
||||
if (filters.withExpired) delete where.validUntil
|
||||
}
|
||||
const [transactionLinks, count] = await DbTransactionLink.findAndCount({
|
||||
where,
|
||||
withDeleted: filters ? filters.withDeleted : false,
|
||||
order: {
|
||||
createdAt: order,
|
||||
},
|
||||
skip: (currentPage - 1) * pageSize,
|
||||
take: pageSize,
|
||||
})
|
||||
|
||||
return {
|
||||
linkCount: count,
|
||||
linkList: transactionLinks.map((tl) => new TransactionLink(tl, new User(user))),
|
||||
}
|
||||
return transactionLinkList(paginated, filters, userId)
|
||||
}
|
||||
}
|
||||
|
||||
const transactionLinkList = async (
|
||||
{ currentPage = 1, pageSize = 5, order = Order.DESC }: Paginated,
|
||||
filters: TransactionLinkFilters | null,
|
||||
userId: number,
|
||||
): Promise<TransactionLinkResult> => {
|
||||
const user = await DbUser.findOneOrFail({ id: userId })
|
||||
const { withDeleted, withExpired, withRedeemed } = filters || {
|
||||
withDeleted: false,
|
||||
withExpired: false,
|
||||
withRedeemed: false,
|
||||
}
|
||||
const [transactionLinks, count] = await DbTransactionLink.findAndCount({
|
||||
where: {
|
||||
userId,
|
||||
...(!withRedeemed && { redeemedBy: null }),
|
||||
...(!withExpired && { validUntil: MoreThan(new Date()) }),
|
||||
},
|
||||
withDeleted,
|
||||
order: {
|
||||
createdAt: order,
|
||||
},
|
||||
skip: (currentPage - 1) * pageSize,
|
||||
take: pageSize,
|
||||
})
|
||||
|
||||
return {
|
||||
count,
|
||||
links: transactionLinks.map((tl) => new TransactionLink(tl, new User(user))),
|
||||
}
|
||||
}
|
||||
|
||||
@ -224,8 +224,8 @@ export const listTransactionLinksAdmin = gql`
|
||||
currentPage: $currentPage
|
||||
pageSize: $pageSize
|
||||
) {
|
||||
linkCount
|
||||
linkList {
|
||||
count
|
||||
links {
|
||||
id
|
||||
amount
|
||||
holdAvailableAmount
|
||||
|
||||
@ -126,14 +126,16 @@ export const queryTransactionLink = gql`
|
||||
export const listTransactionLinks = gql`
|
||||
query($currentPage: Int = 1, $pageSize: Int = 5) {
|
||||
listTransactionLinks(currentPage: $currentPage, pageSize: $pageSize) {
|
||||
id
|
||||
amount
|
||||
holdAvailableAmount
|
||||
memo
|
||||
link
|
||||
createdAt
|
||||
validUntil
|
||||
redeemedAt
|
||||
links {
|
||||
id
|
||||
amount
|
||||
holdAvailableAmount
|
||||
memo
|
||||
link
|
||||
createdAt
|
||||
validUntil
|
||||
redeemedAt
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user