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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | 6x 470x 78x 470x 49x 470x 35x 33x 470x 37x 33x 77x 77x 78x 73x 29x 29x 29x 6x 6x 4x 4x | // Deposit returns (openspec: deposits). A return is written together with
// the rent payments that record the rent kept, all or none, and every
// statement carries the owner check and the tenancy belonging to the flat,
// so a role lost between the read and the write changes nothing. The server
// checks the same rules first to give the reason.
import {
and,
asc,
eq,
exists,
gte,
inArray,
lte,
notExists,
sql,
} from 'drizzle-orm'
import type { IsoDate } from '#/lib/dates'
import { inChunks } from '#/server/db/chunks'
import {
depositReturns,
propertyAccess,
rentPayments,
tenancies,
} from '#/server/db/schema'
import { atomically, type Db } from '#/server/platform/db.server'
export interface DepositReturn {
id: string
tenancyId: string
returnedOn: IsoDate
/** Kept for unpaid rent, cents. */
rentKept: number
/** Kept for damage and cleaning, cents. */
damageKept: number
note: string | null
}
/** A return of one of several flats, for the year summary. */
export interface FlatDepositReturn extends DepositReturn {
propertyId: string
}
/** What an owner enters for a return. */
export interface DepositReturnFields {
returnedOn: IsoDate
rentKept: number
damageKept: number
note: string | null
}
/** A rent payment recorded as kept from the deposit. */
export interface KeptPayment {
month: string
/** Cents. */
amount: number
}
const columns = {
id: depositReturns.id,
tenancyId: depositReturns.tenancyId,
returnedOn: depositReturns.returnedOn,
rentKept: depositReturns.rentKept,
damageKept: depositReturns.damageKept,
note: depositReturns.note,
}
export function depositReturnRepository(db: Db) {
/** The actor owns the flat: the check inside each write. */
const isOwner = (actorId: string, propertyId: string) =>
exists(
db
.select({ one: sql`1` })
.from(propertyAccess)
.where(
and(
eq(propertyAccess.propertyId, propertyId),
eq(propertyAccess.userId, actorId),
eq(propertyAccess.role, 'owner'),
),
),
)
/** The actor owns the flat, and the return belongs to one of its tenancies. */
const ownsReturn = (actorId: string, propertyId: string, returnId: string) =>
and(
isOwner(actorId, propertyId),
exists(
db
.select({ one: sql`1` })
.from(tenancies)
.where(
and(
eq(tenancies.id, depositReturns.tenancyId),
eq(tenancies.propertyId, propertyId),
),
),
),
eq(depositReturns.id, returnId),
)
/** The payments of a return, applied only while the return exists. */
const paymentStatements = (
actorId: string,
propertyId: string,
returnId: string,
payments: KeptPayment[],
now: Date,
) =>
payments.map((payment) =>
db.insert(rentPayments).select(
db
.select({
id: sql<string>`${crypto.randomUUID()}`.as('id'),
tenancyId: depositReturns.tenancyId,
month: sql<string>`${payment.month}`.as('month'),
amount: sql<number>`${payment.amount}`.as('amount'),
paidOn: depositReturns.returnedOn,
depositReturnId: depositReturns.id,
createdBy: sql<string>`${actorId}`.as('created_by'),
createdAt: sql<number>`${now.getTime()}`.as('created_at'),
})
.from(depositReturns)
.where(ownsReturn(actorId, propertyId, returnId)),
),
)
return {
/** The returns of the tenancies, one per tenancy at most. */
async forTenancies(tenancyIds: string[]): Promise<DepositReturn[]> {
return inChunks(tenancyIds, (chunk) =>
db
.select(columns)
.from(depositReturns)
.where(inArray(depositReturns.tenancyId, chunk)),
)
},
/** The return of one tenancy, if any. */
async forTenancy(tenancyId: string): Promise<DepositReturn | undefined> {
const [row] = await db
.select(columns)
.from(depositReturns)
.where(eq(depositReturns.tenancyId, tenancyId))
return row
},
/** The returns of the flats' tenancies dated in `year`, oldest first. */
async forPropertiesYear(
propertyIds: string[],
year: number,
): Promise<FlatDepositReturn[]> {
return inChunks(propertyIds, (chunk) =>
db
.select({ ...columns, propertyId: tenancies.propertyId })
.from(depositReturns)
.innerJoin(tenancies, eq(tenancies.id, depositReturns.tenancyId))
.where(
and(
inArray(tenancies.propertyId, chunk),
gte(depositReturns.returnedOn, `${String(year)}-01-01`),
lte(depositReturns.returnedOn, `${String(year)}-12-31`),
),
)
.orderBy(asc(depositReturns.returnedOn)),
)
},
/**
* Records the return of a tenancy with the payments for the rent kept.
* Null unless the actor owns the flat, the tenancy is the flat's and it
* has no return yet.
*/
async record(
actorId: string,
propertyId: string,
tenancyId: string,
fields: DepositReturnFields,
payments: KeptPayment[],
now: Date,
): Promise<string | null> {
const id = crypto.randomUUID()
const [inserted] = await atomically(db, [
db
.insert(depositReturns)
.select(
db
.select({
id: sql<string>`${id}`.as('id'),
tenancyId: tenancies.id,
returnedOn: sql<string>`${fields.returnedOn}`.as('returned_on'),
rentKept: sql<number>`${fields.rentKept}`.as('rent_kept'),
damageKept: sql<number>`${fields.damageKept}`.as('damage_kept'),
note: sql<string | null>`${fields.note}`.as('note'),
createdBy: sql<string>`${actorId}`.as('created_by'),
createdAt: sql<number>`${now.getTime()}`.as('created_at'),
updatedAt: sql<number>`${now.getTime()}`.as('updated_at'),
})
.from(tenancies)
.where(
and(
eq(tenancies.id, tenancyId),
eq(tenancies.propertyId, propertyId),
isOwner(actorId, propertyId),
notExists(
db
.select({ one: sql`1` })
.from(depositReturns)
.where(eq(depositReturns.tenancyId, tenancyId)),
),
),
),
)
.returning({ id: depositReturns.id }),
...paymentStatements(actorId, propertyId, id, payments, now),
])
return inserted[0]?.id ?? null
},
/**
* Corrects a return: its fields are updated and its payments replaced.
* False unless the actor owns the flat and the return is one of its.
*/
async update(
actorId: string,
propertyId: string,
returnId: string,
fields: DepositReturnFields,
payments: KeptPayment[],
now: Date,
): Promise<boolean> {
const [, updated] = await atomically(db, [
// The payments go first, while the return still exists unchanged:
// each statement repeats the same guard.
db.delete(rentPayments).where(
and(
eq(rentPayments.depositReturnId, returnId),
exists(
db
.select({ one: sql`1` })
.from(depositReturns)
.where(ownsReturn(actorId, propertyId, returnId)),
),
),
),
db
.update(depositReturns)
.set({ ...fields, updatedAt: now })
.where(ownsReturn(actorId, propertyId, returnId))
.returning({ id: depositReturns.id }),
...paymentStatements(actorId, propertyId, returnId, payments, now),
])
return updated.length > 0
},
/** Deletes a return; its payments go with it. False unless allowed. */
async remove(
actorId: string,
propertyId: string,
returnId: string,
): Promise<boolean> {
const [removed] = await atomically(db, [
db
.delete(depositReturns)
.where(ownsReturn(actorId, propertyId, returnId))
.returning({ id: depositReturns.id }),
])
return removed.length > 0
},
}
}
export type DepositReturnRepository = ReturnType<typeof depositReturnRepository>
|