From 853bfabd57bcb2f5ce7375d33f3b9ff57dfeb55d Mon Sep 17 00:00:00 2001 From: elweyn Date: Tue, 20 Dec 2022 12:09:59 +0100 Subject: [PATCH] Finish denie logic, add deniedAt IsNull() to queries. --- backend/src/graphql/model/Contribution.ts | 8 +++++ .../graphql/resolver/ContributionResolver.ts | 30 ++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/backend/src/graphql/model/Contribution.ts b/backend/src/graphql/model/Contribution.ts index cf57e632f..d71d2ce6b 100644 --- a/backend/src/graphql/model/Contribution.ts +++ b/backend/src/graphql/model/Contribution.ts @@ -18,6 +18,8 @@ export class Contribution { this.contributionDate = contribution.contributionDate this.state = contribution.contributionStatus this.messagesCount = contribution.messages ? contribution.messages.length : 0 + this.deniedAt = contribution.deniedAt + this.deniedBy = contribution.deniedBy } @Field(() => Number) @@ -47,6 +49,12 @@ export class Contribution { @Field(() => Number, { nullable: true }) confirmedBy: number | null + @Field(() => Date, { nullable: true }) + deniedAt: Date | null + + @Field(() => Number, { nullable: true }) + deniedBy: number | null + @Field(() => Date) contributionDate: Date diff --git a/backend/src/graphql/resolver/ContributionResolver.ts b/backend/src/graphql/resolver/ContributionResolver.ts index 15edaa9a9..ea12d1362 100644 --- a/backend/src/graphql/resolver/ContributionResolver.ts +++ b/backend/src/graphql/resolver/ContributionResolver.ts @@ -146,6 +146,7 @@ export class ContributionResolver { @Ctx() context: Context, ): Promise { const user = getUser(context) + // TODO: Check if deniedAt IsNull() const where: { userId: number confirmedBy?: FindOperator | null @@ -214,7 +215,7 @@ export class ContributionResolver { const user = getUser(context) const contributionToUpdate = await DbContribution.findOne({ - where: { id: contributionId, confirmedAt: IsNull() }, + where: { id: contributionId, confirmedAt: IsNull(), deniedAt: IsNull() }, }) if (!contributionToUpdate) { logger.error('No contribution found to given id') @@ -406,7 +407,7 @@ export class ContributionResolver { const moderator = getUser(context) const contributionToUpdate = await DbContribution.findOne({ - where: { id, confirmedAt: IsNull() }, + where: { id, confirmedAt: IsNull(), deniedAt: IsNull() }, }) if (!contributionToUpdate) { logger.error('No contribution found to given id.') @@ -472,6 +473,7 @@ export class ContributionResolver { .from(DbContribution, 'c') .leftJoinAndSelect('c.messages', 'm') .where({ confirmedAt: IsNull() }) + .andWhere({ deniedAt: IsNull() }) .getMany() if (contributions.length === 0) { @@ -686,9 +688,29 @@ export class ContributionResolver { @Arg('id', () => Int) id: number, @Ctx() context: Context, ): Promise { - const moderatorUser = getUser(context) - const contributionToUpdate = await DbContribution.findOne({ id }) + // TODO: Check + // - contribution exists + // - state has accept one + if (!contributionToUpdate) { + logger.error(`Contribution not found for given id: ${id}`) + throw new Error(`Contribution not found for given id.`) + } + if ( + contributionToUpdate.contributionStatus !== ContributionStatus.IN_PROGRESS && + contributionToUpdate.contributionStatus !== ContributionStatus.PENDING + ) { + logger.error( + `Contribution state (${contributionToUpdate.contributionStatus}) is not allowed.`, + ) + throw new Error(`State of the contribution is not allowed.`) + } + const user = getUser(context) + + contributionToUpdate.contributionStatus = ContributionStatus.DENIED + contributionToUpdate.deniedBy = user.id + contributionToUpdate.deniedAt = new Date() + await contributionToUpdate.save() return true }