|
| 1 | +import {Quota, QuotaFactory, QuotaIdentifier, Versioned} from "@domain" |
| 2 | +import {Injectable, Logger} from "@nestjs/common" |
| 3 | +import {QuotaCreateError, QuotaGetError, QuotaRepository, QuotaUpdateError} from "@services" |
| 4 | +import * as TE from "fp-ts/lib/TaskEither" |
| 5 | +import {pipe} from "fp-ts/lib/function" |
| 6 | +import {POSTGRES_BIGINT_LOWER_BOUND} from "./constants" |
| 7 | +import {DatabaseClient} from "./database-client" |
| 8 | +import {isPrismaUniqueConstraintError} from "./errors" |
| 9 | + |
| 10 | +@Injectable() |
| 11 | +export class QuotaDbRepository implements QuotaRepository { |
| 12 | + constructor(private readonly dbClient: DatabaseClient) {} |
| 13 | + |
| 14 | + getQuota(identifier: QuotaIdentifier): TE.TaskEither<QuotaGetError, Versioned<Quota>> { |
| 15 | + return pipe( |
| 16 | + TE.tryCatch( |
| 17 | + () => |
| 18 | + this.dbClient.quota.findUnique({ |
| 19 | + where: { |
| 20 | + scope_metric: { |
| 21 | + scope: identifier.scope, |
| 22 | + metric: identifier.metric |
| 23 | + } |
| 24 | + } |
| 25 | + }), |
| 26 | + error => { |
| 27 | + Logger.error("Error retrieving quota", error) |
| 28 | + return "quota_unknown_error" as const |
| 29 | + } |
| 30 | + ), |
| 31 | + TE.chainW(quota => { |
| 32 | + if (!quota) return TE.left("quota_not_found" as const) |
| 33 | + return pipe( |
| 34 | + TE.fromEither(QuotaFactory.validate(quota)), |
| 35 | + TE.map(validQuota => ({...validQuota, occ: quota.occ})) |
| 36 | + ) |
| 37 | + }) |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + createQuota(quota: Quota): TE.TaskEither<QuotaCreateError, Versioned<Quota>> { |
| 42 | + return pipe( |
| 43 | + TE.tryCatch( |
| 44 | + () => |
| 45 | + this.dbClient.quota.create({ |
| 46 | + data: { |
| 47 | + id: quota.id, |
| 48 | + scope: quota.scope, |
| 49 | + metric: quota.metric, |
| 50 | + limit: quota.limit, |
| 51 | + createdAt: quota.createdAt, |
| 52 | + updatedAt: quota.updatedAt, |
| 53 | + occ: POSTGRES_BIGINT_LOWER_BOUND |
| 54 | + } |
| 55 | + }), |
| 56 | + error => { |
| 57 | + if (isPrismaUniqueConstraintError(error, ["scope", "metric"])) return "quota_already_exists" as const |
| 58 | + Logger.error("Error creating quota", error) |
| 59 | + return "quota_unknown_error" as const |
| 60 | + } |
| 61 | + ), |
| 62 | + TE.chainW(createdQuota => |
| 63 | + pipe( |
| 64 | + TE.fromEither(QuotaFactory.validate(createdQuota)), |
| 65 | + TE.map(validQuota => ({...validQuota, occ: createdQuota.occ})) |
| 66 | + ) |
| 67 | + ) |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + updateQuota(quota: Quota, occCheck: bigint): TE.TaskEither<QuotaUpdateError, Versioned<Quota>> { |
| 72 | + return pipe( |
| 73 | + TE.tryCatch( |
| 74 | + async () => { |
| 75 | + return await this.dbClient.$transaction(async tx => { |
| 76 | + const updatedQuotas = await tx.quota.updateManyAndReturn({ |
| 77 | + where: { |
| 78 | + scope: quota.scope, |
| 79 | + metric: quota.metric, |
| 80 | + occ: occCheck |
| 81 | + }, |
| 82 | + data: { |
| 83 | + limit: quota.limit, |
| 84 | + createdAt: quota.createdAt, |
| 85 | + updatedAt: quota.updatedAt, |
| 86 | + occ: {increment: 1} |
| 87 | + } |
| 88 | + }) |
| 89 | + |
| 90 | + if (updatedQuotas.length === 0) { |
| 91 | + // Check if it failed due to OCC or Not Found |
| 92 | + const existing = await tx.quota.findUnique({ |
| 93 | + where: { |
| 94 | + scope_metric: { |
| 95 | + scope: quota.scope, |
| 96 | + metric: quota.metric |
| 97 | + } |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + if (!existing) return "quota_not_found" |
| 102 | + if (existing.occ !== occCheck) return "quota_concurrent_modification_error" |
| 103 | + Logger.error("Error updating quota, quota exists and with the same occ", {existing, occCheck}) |
| 104 | + return "quota_unknown_error" |
| 105 | + } |
| 106 | + |
| 107 | + const item = updatedQuotas[0] |
| 108 | + |
| 109 | + if (!item) { |
| 110 | + Logger.error("Error updating quota, no item returned", {updatedQuotas}) |
| 111 | + return "quota_unknown_error" |
| 112 | + } |
| 113 | + |
| 114 | + return item |
| 115 | + }) |
| 116 | + }, |
| 117 | + error => { |
| 118 | + Logger.error("Error updating quota", error) |
| 119 | + return "quota_unknown_error" as const |
| 120 | + } |
| 121 | + ), |
| 122 | + TE.chainW(result => { |
| 123 | + if (typeof result === "string") return TE.left(result) |
| 124 | + return pipe( |
| 125 | + TE.fromEither(QuotaFactory.validate(result)), |
| 126 | + TE.map(validQuota => ({...validQuota, occ: result.occ})) |
| 127 | + ) |
| 128 | + }) |
| 129 | + ) |
| 130 | + } |
| 131 | +} |
0 commit comments