Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | 60x 7x 7x 5x 5x 5x 3x 3x 2x 2x 2x 65x 1x 1x 2x 2x 2x 2x 2x 54x 1x 1x 2x 2x | // Data Access Layer — Restrict Direct Contact (cohorts + per-pro status + gates)
import { desc, eq } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import type {
NewPrerequisiteGateCheck,
NewRestrictionCohort,
PrerequisiteGateCheck,
ProRestrictionStatus,
RestrictionCohort,
} from "../db/schema";
import * as schema from "../db/schema";
export class ProRestrictionStatusDal {
constructor(private db: DrizzleD1Database<typeof schema>) {}
async get(proId: string): Promise<ProRestrictionStatus | undefined> {
const rows = await this.db
.select()
.from(schema.proRestrictionStatus)
.where(eq(schema.proRestrictionStatus.proId, proId))
.limit(1);
return rows[0];
}
/**
* Set restriction on/off for a pro. Upserts the row — D1 has no transactions,
* so this is a read-then-insert-or-update; a concurrent double-activate simply
* lands on the same terminal state (idempotent).
*/
async setActive(
proId: string,
active: boolean,
opts: { cohortId?: string | null } = {},
): Promise<ProRestrictionStatus> {
const existing = await this.get(proId);
const now = new Date();
if (existing) {
const rows = await this.db
.update(schema.proRestrictionStatus)
.set({
restrictionActive: active,
activatedAt: active
? (existing.activatedAt ?? now)
: existing.activatedAt,
...(opts.cohortId !== undefined && { cohortId: opts.cohortId }),
updatedAt: now,
})
.where(eq(schema.proRestrictionStatus.proId, proId))
.returning();
return rows[0];
}
const rows = await this.db
.insert(schema.proRestrictionStatus)
.values({
proId,
restrictionActive: active,
activatedAt: active ? now : null,
cohortId: opts.cohortId ?? null,
updatedAt: now,
})
.returning();
return rows[0];
}
async listActive(): Promise<ProRestrictionStatus[]> {
return this.db
.select()
.from(schema.proRestrictionStatus)
.where(eq(schema.proRestrictionStatus.restrictionActive, true));
}
}
export class RestrictionCohortsDal {
constructor(private db: DrizzleD1Database<typeof schema>) {}
async create(data: NewRestrictionCohort): Promise<RestrictionCohort> {
const rows = await this.db
.insert(schema.restrictionCohorts)
.values(data)
.returning();
return rows[0];
}
async get(id: string): Promise<RestrictionCohort | undefined> {
const rows = await this.db
.select()
.from(schema.restrictionCohorts)
.where(eq(schema.restrictionCohorts.id, id))
.limit(1);
return rows[0];
}
async list(): Promise<RestrictionCohort[]> {
return this.db
.select()
.from(schema.restrictionCohorts)
.orderBy(desc(schema.restrictionCohorts.cohortNumber));
}
async update(
id: string,
patch: Partial<NewRestrictionCohort>,
): Promise<RestrictionCohort | undefined> {
const rows = await this.db
.update(schema.restrictionCohorts)
.set(patch)
.where(eq(schema.restrictionCohorts.id, id))
.returning();
return rows[0];
}
}
export class PrerequisiteGateChecksDal {
constructor(private db: DrizzleD1Database<typeof schema>) {}
async create(data: NewPrerequisiteGateCheck): Promise<PrerequisiteGateCheck> {
const rows = await this.db
.insert(schema.prerequisiteGateChecks)
.values(data)
.returning();
return rows[0];
}
async latestForCohort(
cohortId: string,
): Promise<PrerequisiteGateCheck | undefined> {
const rows = await this.db
.select()
.from(schema.prerequisiteGateChecks)
.where(eq(schema.prerequisiteGateChecks.cohortId, cohortId))
// id tiebreaker: checkedAt has second resolution (unixepoch), so two
// checks run in the same second would otherwise tie and "latest" could
// return the older one — caught in live QA when a failing check and a
// passing re-check landed in the same second.
.orderBy(
desc(schema.prerequisiteGateChecks.checkedAt),
desc(schema.prerequisiteGateChecks.id),
)
.limit(1);
return rows[0];
}
}
|