mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 09:56:05 +00:00
update interactions, better code
This commit is contained in:
parent
1165b346eb
commit
e7ad986389
@ -4,6 +4,7 @@ import { Decimal } from 'decimal.js-light'
|
||||
|
||||
import { Role } from '@/auth/Role'
|
||||
import { ContributionLogic } from '@/data/Contribution.logic'
|
||||
import { Context, getClientTimezoneOffset } from '@/server/context'
|
||||
import { LogError } from '@/server/LogError'
|
||||
|
||||
export abstract class UnconfirmedContributionRole {
|
||||
@ -21,9 +22,9 @@ export abstract class UnconfirmedContributionRole {
|
||||
|
||||
// steps which return void throw on each error
|
||||
// first, check if it can be updated
|
||||
public abstract checkAuthorization(user: User, role: Role): void
|
||||
protected abstract checkAuthorization(user: User, role: Role): void
|
||||
// second, check if contribution is still valid after update
|
||||
public async validate(clientTimezoneOffset: number): Promise<void> {
|
||||
protected async validate(clientTimezoneOffset: number): Promise<void> {
|
||||
// TODO: remove this restriction
|
||||
if (this.self.contributionDate.getMonth() !== this.updatedCreationDate.getMonth()) {
|
||||
throw new LogError('Month of contribution can not be changed')
|
||||
@ -42,7 +43,16 @@ export abstract class UnconfirmedContributionRole {
|
||||
}
|
||||
|
||||
// third, actually update entity
|
||||
public abstract update(): void
|
||||
protected abstract update(): void
|
||||
|
||||
public async checkAndUpdate(context: Context): Promise<void> {
|
||||
if (!context.user || !context.role) {
|
||||
throw new LogError('missing user or role on context')
|
||||
}
|
||||
this.checkAuthorization(context.user, context.role)
|
||||
await this.validate(getClientTimezoneOffset(context))
|
||||
this.update()
|
||||
}
|
||||
|
||||
public getAvailableCreationSums(): Decimal[] {
|
||||
if (!this.availableCreationSums) {
|
||||
|
||||
@ -18,7 +18,7 @@ export class UnconfirmedContributionAdminRole extends UnconfirmedContributionRol
|
||||
super(contribution, updateData.amount, new Date(updateData.creationDate))
|
||||
}
|
||||
|
||||
public update(): void {
|
||||
protected update(): void {
|
||||
this.self.amount = this.updateData.amount
|
||||
this.self.memo = this.updateData.memo
|
||||
this.self.contributionDate = new Date(this.updateData.creationDate)
|
||||
@ -28,7 +28,7 @@ export class UnconfirmedContributionAdminRole extends UnconfirmedContributionRol
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
public checkAuthorization(user: User, role: Role): UnconfirmedContributionRole {
|
||||
protected checkAuthorization(user: User, role: Role): UnconfirmedContributionRole {
|
||||
if (
|
||||
!role.hasRight(RIGHTS.MODERATOR_UPDATE_CONTRIBUTION_MEMO) &&
|
||||
this.self.moderatorId === null
|
||||
|
||||
@ -12,7 +12,7 @@ export class UnconfirmedContributionUserRole extends UnconfirmedContributionRole
|
||||
super(contribution, updateData.amount, new Date(updateData.creationDate))
|
||||
}
|
||||
|
||||
public update(): void {
|
||||
protected update(): void {
|
||||
this.self.amount = this.updateData.amount
|
||||
this.self.memo = this.updateData.memo
|
||||
this.self.contributionDate = new Date(this.updateData.creationDate)
|
||||
@ -22,7 +22,7 @@ export class UnconfirmedContributionUserRole extends UnconfirmedContributionRole
|
||||
this.self.updatedBy = null
|
||||
}
|
||||
|
||||
public checkAuthorization(user: User): UnconfirmedContributionRole {
|
||||
protected checkAuthorization(user: User): UnconfirmedContributionRole {
|
||||
if (this.self.userId !== user.id) {
|
||||
throw new LogError('Can not update contribution of another user', this.self, user.id)
|
||||
}
|
||||
|
||||
@ -70,10 +70,8 @@ export class UpdateUnconfirmedContributionContext {
|
||||
throw new LogError("don't recognize input type, maybe not implemented yet?")
|
||||
}
|
||||
// run steps
|
||||
// all possible cases not to be true are thrown in the next functions
|
||||
unconfirmedContributionRole.checkAuthorization(this.context.user, this.context.role)
|
||||
await unconfirmedContributionRole.validate(getClientTimezoneOffset(this.context))
|
||||
unconfirmedContributionRole.update()
|
||||
// all possible cases not to be true are thrown in the next function
|
||||
await unconfirmedContributionRole.checkAndUpdate(this.context)
|
||||
|
||||
return {
|
||||
contribution: contributionToUpdate,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user