Finish denie logic, add deniedAt IsNull() to queries.

This commit is contained in:
elweyn 2022-12-20 12:09:59 +01:00
parent dabef05dc0
commit 853bfabd57
2 changed files with 34 additions and 4 deletions

View File

@ -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

View File

@ -146,6 +146,7 @@ export class ContributionResolver {
@Ctx() context: Context,
): Promise<ContributionListResult> {
const user = getUser(context)
// TODO: Check if deniedAt IsNull()
const where: {
userId: number
confirmedBy?: FindOperator<number> | 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<boolean> {
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
}