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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 | 17x 867x 867x 903x 867x 403x 867x 15x 867x 32x 867x 62x 382x 382x 382x 382x 867x 459x 867x 62x 43x 43x 867x 192x 192x 192x 192x 1x 191x 4x 187x 187x 187x 187x 173x 14x 1x 1x 1x 867x 248x 326x 5x 321x 268x 18x 250x 252x 250x 250x 420x 250x 247x 19x 228x 230x 228x 228x 378x 228x 268x 18x 250x 252x 250x 192x 58x 509x 62x 58x 58x 509x 96911x 524x 58x 42x 42x 42x 42x 42x 14x 14x 14x 5x 5x 5x 2x 2x 3x 3x 4x 4x 4x 1x 1x 4x 4x 4x 4x 4x 52x 52x 174x 174x 11x 4x 7x 150x 150x 1x 150x 232x 232x 232x 15x 15x 15x 15x 1x 15x 32x 32x 9x 9x 6x 6x | // A flat's tenancies (openspec: tenancies). Each write carries the owner
// check and the no-overlap rule in its own statement, so a role lost or a
// tenancy added between the read and the write changes nothing. The server
// checks the same rules first to give the reason.
import {
and,
asc,
desc,
eq,
exists,
gt,
gte,
inArray,
isNotNull,
isNull,
lt,
lte,
ne,
notExists,
or,
sql,
type SQL,
} from 'drizzle-orm'
import type { BatchItem } from 'drizzle-orm/batch'
import { alias } from 'drizzle-orm/sqlite-core'
import { dayBefore, type IsoDate } from '#/lib/dates'
import { inChunks } from '#/server/db/chunks'
import {
properties,
propertyAccess,
rentPayments,
tenancies,
tenancyOccupants,
tenancyRents,
tenantFeeAmounts,
tenantFees,
type ChargeBasis,
type ChargeKind,
type ContractType,
type NoticeGiver,
} from '#/server/db/schema'
import type { Period } from '#/features/recurring-amounts/periods'
import { atomically, type Db } from '#/server/platform/db.server'
import {
occupantsPeriods,
periodUndoStatements,
tenantFeePeriods,
periodWriteStatements,
} from '#/server/db/periods'
/** What an owner enters for a tenancy. */
export interface TenancyFields {
tenantName: string
tenantPhone: string | null
tenantEmail: string | null
contractType: ContractType
contractMade: IsoDate
/** The start. */
validFrom: IsoDate
/** A fixed term's end; null for an open-ended tenancy. */
termEnd: IsoDate | null
dueDay: number
/** The deposit in cents; null or left out when none is set. */
deposit?: number | null
}
export interface Tenancy extends TenancyFields {
id: string
propertyId: string
/** The last day; null while an open-ended tenancy runs. */
validTo: IsoDate | null
noticeGivenBy: NoticeGiver | null
noticeDate: IsoDate | null
}
/** How a tenancy is ended. */
export interface Ending {
noticeGivenBy: NoticeGiver
noticeDate: IsoDate
lastDay: IsoDate
}
/**
* A rent write's expectation: the tenancy's latest rent period's start as
* read, or none, so a concurrent change makes the write apply nothing.
*/
export interface RentWrite {
actorId: string
propertyId: string
tenancyId: string
/** Cents per month. */
amount: number
from: IsoDate
latestStart: IsoDate | null
/**
* True when `from` is the start of one of the tenancy's periods: the
* write corrects that period's amount, while it still starts there.
*/
corrects?: boolean
/**
* The first period's start as read, when `from` is before it: the write
* fills the months up to it instead of changing the latest period.
*/
firstStart?: IsoDate
}
/** An occupants write's expectation: the latest period's start as read. */
export interface OccupantsWrite {
actorId: string
propertyId: string
tenancyId: string
persons: number
from: IsoDate
latestStart: IsoDate | null
/** True when `from` is the latest period's start: it is corrected. */
corrects?: boolean
}
/** A new tenant fee with its first amount from `from`. */
export interface NewFee {
actorId: string
propertyId: string
tenancyId: string
kind: ChargeKind
basis: ChargeBasis
/** Set for the kind `other`. */
description: string | null
/** Cents per month or per occupant per month. */
amount: number
from: IsoDate
}
/** A fee amount write's expectation: the latest period's start as read. */
export interface FeeAmountWrite {
actorId: string
propertyId: string
tenancyId: string
feeId: string
amount: number
from: IsoDate
latestStart: IsoDate
/** True when `from` is the latest period's start: it is corrected. */
corrects?: boolean
}
/** A fee undo's expectation; no previous start means removing the fee. */
export interface FeeUndo {
actorId: string
propertyId: string
tenancyId: string
feeId: string
latestStart: IsoDate
previousStart: IsoDate | null
}
/** An undo's expectation: the latest and previous rent periods' starts. */
export interface RentUndo {
actorId: string
propertyId: string
tenancyId: string
latestStart: IsoDate
previousStart: IsoDate
}
/** A fee the tenant pays on top of the rent, with its amount periods. */
export interface TenantFee {
id: string
tenancyId: string
kind: ChargeKind
basis: ChargeBasis
/** Set for the kind `other`. */
description: string | null
/** Cents per month or per occupant per month, oldest first. */
amounts: Period<number>[]
}
const columns = {
id: tenancies.id,
propertyId: tenancies.propertyId,
tenantName: tenancies.tenantName,
tenantPhone: tenancies.tenantPhone,
tenantEmail: tenancies.tenantEmail,
contractType: tenancies.contractType,
contractMade: tenancies.contractMade,
validFrom: tenancies.validFrom,
validTo: tenancies.validTo,
termEnd: tenancies.termEnd,
dueDay: tenancies.dueDay,
noticeGivenBy: tenancies.noticeGivenBy,
noticeDate: tenancies.noticeDate,
deposit: tenancies.deposit,
}
export function tenancyRepository(db: Db) {
const other = alias(tenancies, 'other_tenancy')
/** 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'),
),
),
)
/**
* No other tenancy of the flat overlaps `from`–`to` (none: indefinitely).
* `except` leaves out the tenancy being changed.
*/
const noOverlap = (
propertyId: string,
from: IsoDate,
to: IsoDate | null,
except?: string,
): SQL | undefined =>
notExists(
db
.select({ one: sql`1` })
.from(other)
.where(
and(
eq(other.propertyId, propertyId),
except === undefined ? undefined : ne(other.id, except),
to === null ? undefined : lte(other.validFrom, to),
or(isNull(other.validTo), gte(other.validTo, from)),
),
),
)
/**
* None of the tenancy's rent payments falls outside the months of
* `from`–`to` (none: indefinitely), so payments stay inside their tenancy.
*/
const paymentsInside = (from: IsoDate, to: IsoDate | null): SQL =>
notExists(
db
.select({ one: sql`1` })
.from(rentPayments)
.where(
and(
eq(rentPayments.tenancyId, tenancies.id),
or(
lt(rentPayments.month, from.slice(0, 7)),
to === null ? undefined : gt(rentPayments.month, to.slice(0, 7)),
),
),
),
)
/** No rent payment of the tenancy is for a month after `lastDay`'s. */
const paymentsUntil = (lastDay: IsoDate): SQL =>
notExists(
db
.select({ one: sql`1` })
.from(rentPayments)
.where(
and(
eq(rentPayments.tenancyId, tenancies.id),
gt(rentPayments.month, lastDay.slice(0, 7)),
),
),
)
const ownTenancy = (actorId: string, propertyId: string, id: string) =>
and(
eq(tenancies.id, id),
eq(tenancies.propertyId, propertyId),
isOwner(actorId, propertyId),
)
/**
* The insert of a new tenancy, for the caller's atomically() batch: it
* writes nothing unless the actor owns the flat and nothing overlaps.
*/
function createStatement(
actorId: string,
propertyId: string,
fields: TenancyFields,
now: Date,
ending?: Ending,
) {
const id = crypto.randomUUID()
// The last day: a recorded ending's, else the fixed term's end.
const validTo = ending?.lastDay ?? fields.termEnd
const statement = db
.insert(tenancies)
.select(
db
.select({
id: sql<string>`${id}`.as('id'),
propertyId: properties.id,
tenantName: sql<string>`${fields.tenantName}`.as('tenant_name'),
tenantPhone: sql<string | null>`${fields.tenantPhone}`.as(
'tenant_phone',
),
tenantEmail: sql<string | null>`${fields.tenantEmail}`.as(
'tenant_email',
),
contractType: sql<ContractType>`${fields.contractType}`.as(
'contract_type',
),
contractMade: sql<string>`${fields.contractMade}`.as(
'contract_made',
),
validFrom: sql<string>`${fields.validFrom}`.as('valid_from'),
validTo: sql<string | null>`${validTo}`.as('valid_to'),
termEnd: sql<string | null>`${fields.termEnd}`.as('term_end'),
dueDay: sql<number>`${fields.dueDay}`.as('due_day'),
noticeGivenBy: sql<NoticeGiver | null>`${
ending?.noticeGivenBy ?? null
}`.as('notice_given_by'),
noticeDate: sql<string | null>`${ending?.noticeDate ?? null}`.as(
'notice_date',
),
deposit: sql<number | null>`${fields.deposit ?? null}`.as(
'deposit',
),
createdBy: sql<string>`${actorId}`.as('created_by'),
createdAt: sql<number>`${now.getTime()}`.as('created_at'),
updatedAt: sql<number>`${now.getTime()}`.as('updated_at'),
})
.from(properties)
.where(
and(
eq(properties.id, propertyId),
isOwner(actorId, propertyId),
noOverlap(propertyId, fields.validFrom, validTo),
),
),
)
.returning({ id: tenancies.id })
return { id, statement }
}
/** The actor owns the flat, and the tenancy is the flat's. */
const ownsTenancy = (
actorId: string,
propertyId: string,
tenancyId: string,
) =>
and(
isOwner(actorId, propertyId),
exists(
db
.select({ one: sql`1` })
.from(other)
.where(
and(eq(other.id, tenancyId), eq(other.propertyId, propertyId)),
),
),
)
/** The fee is the tenancy's and the actor owns the tenancy's flat. */
const ownsFee = (
actorId: string,
propertyId: string,
tenancyId: string,
feeId: string,
) =>
and(
ownsTenancy(actorId, propertyId, tenancyId),
exists(
db
.select({ one: sql`1` })
.from(tenantFees)
.where(
and(eq(tenantFees.id, feeId), eq(tenantFees.tenancyId, tenancyId)),
),
),
)
/**
* The statements that add a tenant fee with its first amount period: the
* period goes in only if the fee did, and the fee only if the actor owns
* the tenancy's flat. The first returns the fee's id when it applied.
*/
function feeStatements(
fee: NewFee,
feeId: string,
): [BatchItem<'sqlite'>, ...BatchItem<'sqlite'>[]] {
const { actorId, propertyId, tenancyId } = fee
return [
db
.insert(tenantFees)
.select(
db
.select({
id: sql<string>`${feeId}`.as('id'),
tenancyId: tenancies.id,
kind: sql<ChargeKind>`${fee.kind}`.as('kind'),
basis: sql<ChargeBasis>`${fee.basis}`.as('basis'),
description: sql<string | null>`${fee.description}`.as(
'description',
),
})
.from(tenancies)
.where(
and(
eq(tenancies.id, tenancyId),
ownsTenancy(actorId, propertyId, tenancyId),
),
),
)
.returning({ id: tenantFees.id }),
...periodWriteStatements(db, tenantFeePeriods, {
parentId: feeId,
value: fee.amount,
from: fee.from,
latestStart: null,
owned: ownsFee(actorId, propertyId, tenancyId, feeId),
}),
]
}
const rentsOf = (tenancyId: string) => eq(tenancyRents.tenancyId, tenancyId)
/**
* The statements that set a tenancy's rent from `from`: on a period's
* start (`corrects`) they correct it, otherwise they close the latest period
* the day before and add a new one; before the first period's start they
* add a period up to it. `onlyIf` joins every statement's condition. The
* last statement returns a row when it applied.
*/
function rentStatements(
write: RentWrite,
onlyIf?: SQL,
): [BatchItem<'sqlite'>, ...BatchItem<'sqlite'>[]] {
const { actorId, propertyId, tenancyId, amount, from, latestStart } = write
const scope = rentsOf(tenancyId)
const owned = ownsTenancy(actorId, propertyId, tenancyId)
if (write.firstStart !== undefined) {
return [fillStatement(write, write.firstStart, owned, onlyIf)]
}
if (write.corrects === true) {
return [
db
.update(tenancyRents)
.set({ amount })
.where(and(scope, eq(tenancyRents.validFrom, from), owned, onlyIf))
.returning({ id: tenancyRents.id }),
]
}
const id = crypto.randomUUID()
// The latest period is closed by this write (or there was none), and
// nothing starts on or after `from`.
const ready = and(
eq(tenancies.id, tenancyId),
owned,
onlyIf,
notExists(
db
.select({ one: sql`1` })
.from(tenancyRents)
.where(and(scope, gte(tenancyRents.validFrom, from))),
),
latestStart === null
? notExists(
db
.select({ one: sql`1` })
.from(tenancyRents)
.where(scope),
)
: exists(
db
.select({ one: sql`1` })
.from(tenancyRents)
.where(
and(
scope,
eq(tenancyRents.validFrom, latestStart),
eq(tenancyRents.validTo, dayBefore(from)),
),
),
),
)
const insert = db
.insert(tenancyRents)
.select(
db
.select({
id: sql<string>`${id}`.as('id'),
tenancyId: tenancies.id,
amount: sql<number>`${amount}`.as('amount'),
validFrom: sql<string>`${from}`.as('valid_from'),
validTo: sql<null>`null`.as('valid_to'),
})
.from(tenancies)
.where(ready),
)
.returning({ id: tenancyRents.id })
if (latestStart === null) {
return [insert]
}
return [
db
.update(tenancyRents)
.set({ validTo: dayBefore(from) })
.where(
and(
scope,
eq(tenancyRents.validFrom, latestStart),
lt(tenancyRents.validFrom, from),
isNull(tenancyRents.validTo),
owned,
onlyIf,
),
),
insert,
]
}
/**
* Adds a period from `write.from` to the day before the first period,
* while the first period still starts on `write.firstStart`.
*/
function fillStatement(
write: RentWrite,
firstStart: IsoDate,
owned: SQL | undefined,
onlyIf?: SQL,
): BatchItem<'sqlite'> {
const { tenancyId, amount, from } = write
const scope = rentsOf(tenancyId)
return db
.insert(tenancyRents)
.select(
db
.select({
id: sql<string>`${crypto.randomUUID()}`.as('id'),
tenancyId: tenancies.id,
amount: sql<number>`${amount}`.as('amount'),
validFrom: sql<string>`${from}`.as('valid_from'),
validTo: sql<string>`${dayBefore(firstStart)}`.as('valid_to'),
})
.from(tenancies)
.where(
and(
eq(tenancies.id, tenancyId),
owned,
onlyIf,
exists(
db
.select({ one: sql`1` })
.from(tenancyRents)
.where(and(scope, eq(tenancyRents.validFrom, firstStart))),
),
notExists(
db
.select({ one: sql`1` })
.from(tenancyRents)
.where(and(scope, lt(tenancyRents.validFrom, firstStart))),
),
),
),
)
.returning({ id: tenancyRents.id })
}
return {
/** The flat's tenancies, newest first. */
forProperty(propertyId: string): Promise<Tenancy[]> {
return this.forProperties([propertyId])
},
/** The tenancies of the flats, newest first. */
async forProperties(propertyIds: string[]): Promise<Tenancy[]> {
if (propertyIds.length === 0) {
return []
}
return db
.select(columns)
.from(tenancies)
.where(inArray(tenancies.propertyId, propertyIds))
.orderBy(desc(tenancies.validFrom))
},
/**
* The tenancies' rent periods by tenancy id, oldest first (openspec:
* tenancies, "The rent of a tenancy"). A tenancy without a rent has no
* entry.
*/
async rentsFor(
tenancyIds: string[],
): Promise<Partial<Record<string, Period<number>[]>>> {
if (tenancyIds.length === 0) {
return {}
}
const rows = await inChunks(tenancyIds, (chunk) =>
db
.select()
.from(tenancyRents)
.where(inArray(tenancyRents.tenancyId, chunk))
.orderBy(asc(tenancyRents.validFrom)),
)
const rents: Partial<Record<string, Period<number>[]>> = {}
for (const row of rows) {
;(rents[row.tenancyId] ??= []).push({
validFrom: row.validFrom,
validTo: row.validTo,
value: row.amount,
})
}
return rents
},
/**
* The occupants of tenancies as periods, oldest first (openspec:
* tenancies, "Occupants of a tenancy"). A tenancy without any has no
* entry.
*/
async occupantsFor(
tenancyIds: string[],
): Promise<Partial<Record<string, Period<number>[]>>> {
if (tenancyIds.length === 0) {
return {}
}
const rows = await inChunks(tenancyIds, (chunk) =>
db
.select()
.from(tenancyOccupants)
.where(inArray(tenancyOccupants.tenancyId, chunk))
.orderBy(asc(tenancyOccupants.validFrom)),
)
const occupants: Partial<Record<string, Period<number>[]>> = {}
for (const row of rows) {
;(occupants[row.tenancyId] ??= []).push({
validFrom: row.validFrom,
validTo: row.validTo,
value: row.persons,
})
}
return occupants
},
/**
* The tenant fees of tenancies with their amount periods (openspec:
* tenancies, "Tenant fees are recorded as periods"). A tenancy without
* fees has no entry.
*/
async feesFor(
tenancyIds: string[],
): Promise<Partial<Record<string, TenantFee[]>>> {
if (tenancyIds.length === 0) {
return {}
}
const feeRows = await inChunks(tenancyIds, (chunk) =>
db
.select()
.from(tenantFees)
.where(inArray(tenantFees.tenancyId, chunk)),
)
if (feeRows.length === 0) {
return {}
}
const amountRows = await inChunks(
feeRows.map((fee) => fee.id),
(chunk) =>
db
.select()
.from(tenantFeeAmounts)
.where(inArray(tenantFeeAmounts.tenantFeeId, chunk))
.orderBy(asc(tenantFeeAmounts.validFrom)),
)
const fees: Partial<Record<string, TenantFee[]>> = {}
for (const row of feeRows) {
;(fees[row.tenancyId] ??= []).push({
...row,
amounts: amountRows
.filter((amount) => amount.tenantFeeId === row.id)
.map((amount) => ({
validFrom: amount.validFrom,
validTo: amount.validTo,
value: amount.amount,
})),
})
}
return fees
},
/**
* Sets a tenancy's rent from `write.from` (openspec: tenancies,
* "Changing a tenancy's rent"). False when nothing applied: the actor
* isn't an owner, the tenancy isn't the flat's, or the latest period
* changed since it was read.
*/
async setRent(write: RentWrite): Promise<boolean> {
const results = await atomically(db, rentStatements(write))
return (results.at(-1) as unknown[]).length > 0
},
/**
* Adds a tenant fee with its first amount period, both or neither: the
* period goes in only if the fee did. Returns the fee's id, or null when
* the actor isn't an owner or the tenancy isn't the flat's.
*/
async addFee(fee: NewFee): Promise<string | null> {
const feeId = crypto.randomUUID()
const results = await atomically(db, feeStatements(fee, feeId))
return (results[0] as unknown[]).length > 0 ? feeId : null
},
/**
* Gives a tenant fee a new amount from `write.from`: a new period, or a
* correction of the latest. False when nothing applied.
*/
async setFeeAmount(write: FeeAmountWrite): Promise<boolean> {
const { actorId, propertyId, tenancyId, feeId } = write
const results = await atomically(
db,
periodWriteStatements(db, tenantFeePeriods, {
parentId: feeId,
value: write.amount,
from: write.from,
latestStart: write.latestStart,
corrects: write.corrects ?? false,
owned: ownsFee(actorId, propertyId, tenancyId, feeId),
}),
)
return (results.at(-1) as unknown[]).length > 0
},
/**
* Undoes a tenant fee's latest change: deletes the latest period and
* reopens the previous one, or removes the fee with its only period.
* False when nothing applied.
*/
async undoFeeChange(undo: FeeUndo): Promise<boolean> {
const { actorId, propertyId, tenancyId, feeId, latestStart } = undo
const owned = ownsFee(actorId, propertyId, tenancyId, feeId)
if (undo.previousStart !== null) {
const results = await atomically(
db,
periodUndoStatements(db, tenantFeePeriods, {
parentId: feeId,
latestStart,
previousStart: undo.previousStart,
owned,
}),
)
return (results[0] as unknown[]).length > 0
}
const [removed] = await atomically(db, [
db
.delete(tenantFees)
.where(
and(
eq(tenantFees.id, feeId),
eq(tenantFees.tenancyId, tenancyId),
ownsTenancy(actorId, propertyId, tenancyId),
// Only the one period, still the latest.
exists(
db
.select({ one: sql`1` })
.from(tenantFeeAmounts)
.where(
and(
eq(tenantFeeAmounts.tenantFeeId, feeId),
eq(tenantFeeAmounts.validFrom, latestStart),
),
),
),
notExists(
db
.select({ one: sql`1` })
.from(tenantFeeAmounts)
.where(
and(
eq(tenantFeeAmounts.tenantFeeId, feeId),
ne(tenantFeeAmounts.validFrom, latestStart),
),
),
),
),
)
.returning({ id: tenantFees.id }),
])
return removed.length > 0
},
/**
* Sets the tenancy's occupants from `write.from`: a new period, or a
* correction of the latest. False when nothing applied: the actor isn't
* an owner, the tenancy isn't the flat's, or the latest period changed
* since it was read.
*/
async setOccupants(write: OccupantsWrite): Promise<boolean> {
const { actorId, propertyId, tenancyId, persons, from } = write
const results = await atomically(
db,
periodWriteStatements(db, occupantsPeriods, {
parentId: tenancyId,
value: persons,
from,
latestStart: write.latestStart,
corrects: write.corrects ?? false,
owned: ownsTenancy(actorId, propertyId, tenancyId),
}),
)
return (results.at(-1) as unknown[]).length > 0
},
/**
* Undoes the tenancy's latest occupants change: deletes the latest
* period and reopens the previous one. False when nothing applied.
*/
async undoOccupants(undo: RentUndo): Promise<boolean> {
const results = await atomically(
db,
periodUndoStatements(db, occupantsPeriods, {
parentId: undo.tenancyId,
latestStart: undo.latestStart,
previousStart: undo.previousStart,
owned: ownsTenancy(undo.actorId, undo.propertyId, undo.tenancyId),
}),
)
return (results[0] as unknown[]).length > 0
},
/**
* Undoes a tenancy's latest rent change: deletes the latest period and
* reopens the previous one. The first period is never deleted, because
* the previous one must be there. False when nothing applied.
*/
async undoLatestRent(undo: RentUndo): Promise<boolean> {
const { actorId, propertyId, tenancyId, latestStart, previousStart } =
undo
const scope = rentsOf(tenancyId)
const previous = and(
scope,
eq(tenancyRents.validFrom, previousStart),
eq(tenancyRents.validTo, dayBefore(latestStart)),
)
const [removed] = await atomically(db, [
db
.delete(tenancyRents)
.where(
and(
scope,
eq(tenancyRents.validFrom, latestStart),
isNull(tenancyRents.validTo),
ownsTenancy(actorId, propertyId, tenancyId),
exists(
db
.select({ one: sql`1` })
.from(tenancyRents)
.where(previous),
),
),
)
.returning({ id: tenancyRents.id }),
db
.update(tenancyRents)
.set({ validTo: null })
.where(
and(
previous,
notExists(
db
.select({ one: sql`1` })
.from(tenancyRents)
.where(and(scope, eq(tenancyRents.validFrom, latestStart))),
),
),
),
])
return removed.length > 0
},
/** The months (`YYYY-MM`) the tenancy has rent payments for. */
async paymentMonths(tenancyId: string): Promise<string[]> {
const rows = await db
.selectDistinct({ month: rentPayments.month })
.from(rentPayments)
.where(eq(rentPayments.tenancyId, tenancyId))
.orderBy(asc(rentPayments.month))
return rows.map((row) => row.month)
},
/** A tenancy, or null. */
async get(tenancyId: string): Promise<Tenancy | null> {
const row = await db
.select(columns)
.from(tenancies)
.where(eq(tenancies.id, tenancyId))
.get()
return row ?? null
},
/**
* The flats' tenancies that haven't ended before `on`, oldest first: the
* running one, then those to come.
*/
async currentFor(propertyIds: string[], on: IsoDate): Promise<Tenancy[]> {
if (propertyIds.length === 0) {
return []
}
return db
.select(columns)
.from(tenancies)
.where(
and(
inArray(tenancies.propertyId, propertyIds),
or(isNull(tenancies.validTo), gte(tenancies.validTo, on)),
),
)
.orderBy(asc(tenancies.validFrom))
},
/**
* Adds a tenancy with its first rent period and its first occupants
* period from its start, all or none: they go in only if the tenancy row
* did. `overlaps` also covers a lost owner role.
*/
async createWithRent(
actorId: string,
propertyId: string,
fields: TenancyFields,
/** Cents per month. */
rent: number,
now: Date,
/** An ending recorded with the tenancy, for an earlier one. */
ending?: Ending,
/** The persons living in the flat from the start; one by default. */
occupants = 1,
/** Fees added from the start day, e.g. copied from the previous tenancy. */
fees: Pick<NewFee, 'kind' | 'basis' | 'description' | 'amount'>[] = [],
): Promise<{ outcome: 'created'; id: string } | { outcome: 'overlaps' }> {
const { id, statement } = createStatement(
actorId,
propertyId,
fields,
now,
ending,
)
const [inserted] = await atomically(db, [
statement,
...rentStatements({
actorId,
propertyId,
tenancyId: id,
amount: rent,
from: fields.validFrom,
latestStart: null,
}),
...periodWriteStatements(db, occupantsPeriods, {
parentId: id,
value: occupants,
from: fields.validFrom,
latestStart: null,
owned: ownsTenancy(actorId, propertyId, id),
}),
...fees.flatMap((fee) =>
feeStatements(
{
...fee,
actorId,
propertyId,
tenancyId: id,
from: fields.validFrom,
},
crypto.randomUUID(),
),
),
])
return inserted.length > 0
? { outcome: 'created', id }
: { outcome: 'overlaps' }
},
/** Adds a tenancy. Null when the actor isn't an owner or it overlaps. */
async create(
actorId: string,
propertyId: string,
fields: TenancyFields,
now: Date,
): Promise<string | null> {
const { statement } = createStatement(actorId, propertyId, fields, now)
const [inserted] = await atomically(db, [statement])
return inserted[0]?.id ?? null
},
/**
* Corrects a tenancy in place. `validTo` is its last day after the
* correction, and the write applies only while the stored last day is
* still `expectedValidTo`, so an ending recorded meanwhile isn't lost.
*/
async update(
actorId: string,
propertyId: string,
tenancyId: string,
fields: TenancyFields,
change: {
validTo: IsoDate | null
expectedValidTo: IsoDate | null
/** The first rent period's move with a corrected start, or none. */
firstRent?: { from: IsoDate; to: IsoDate } | null
/** The first occupants period's move with a corrected start. */
firstOccupants?: { from: IsoDate; to: IsoDate } | null
/** Each fee's first amount period's move with a corrected start. */
firstFees?: { feeId: string; from: IsoDate; to: IsoDate }[]
},
now: Date,
): Promise<boolean> {
const move = change.firstRent
const occupantsMove = change.firstOccupants
// Only once the tenancy has its new start, i.e. the update applied.
const hasNewStart = exists(
db
.select({ one: sql`1` })
.from(other)
.where(
and(eq(other.id, tenancyId), eq(other.validFrom, fields.validFrom)),
),
)
const [updated] = await atomically(db, [
db
.update(tenancies)
.set({ ...fields, validTo: change.validTo, updatedAt: now })
.where(
and(
ownTenancy(actorId, propertyId, tenancyId),
change.expectedValidTo === null
? isNull(tenancies.validTo)
: eq(tenancies.validTo, change.expectedValidTo),
noOverlap(
propertyId,
fields.validFrom,
change.validTo,
tenancyId,
),
paymentsInside(fields.validFrom, change.validTo),
),
)
.returning({ id: tenancies.id }),
// Only once the tenancy has its new start, i.e. the update applied.
...(move
? [
db
.update(tenancyRents)
.set({ validFrom: move.to })
.where(
and(
rentsOf(tenancyId),
eq(tenancyRents.validFrom, move.from),
exists(
db
.select({ one: sql`1` })
.from(other)
.where(
and(
eq(other.id, tenancyId),
eq(other.validFrom, fields.validFrom),
),
),
),
),
),
]
: []),
...(occupantsMove
? [
db
.update(tenancyOccupants)
.set({ validFrom: occupantsMove.to })
.where(
and(
eq(tenancyOccupants.tenancyId, tenancyId),
eq(tenancyOccupants.validFrom, occupantsMove.from),
hasNewStart,
),
),
]
: []),
...(change.firstFees ?? []).map((fee) =>
db
.update(tenantFeeAmounts)
.set({ validFrom: fee.to })
.where(
and(
eq(tenantFeeAmounts.tenantFeeId, fee.feeId),
eq(tenantFeeAmounts.validFrom, fee.from),
hasNewStart,
),
),
),
])
return updated.length > 0
},
/** Ends a tenancy that hasn't been ended. */
async end(
actorId: string,
propertyId: string,
tenancyId: string,
ending: Ending,
now: Date,
): Promise<boolean> {
const [updated] = await atomically(db, [
db
.update(tenancies)
.set({
validTo: ending.lastDay,
noticeGivenBy: ending.noticeGivenBy,
noticeDate: ending.noticeDate,
updatedAt: now,
})
.where(
and(
ownTenancy(actorId, propertyId, tenancyId),
isNull(tenancies.noticeGivenBy),
paymentsUntil(ending.lastDay),
),
)
.returning({ id: tenancies.id }),
])
return updated.length > 0
},
/**
* Removes a tenancy that starts after `today`. False when it has started,
* isn't the flat's or the actor isn't an owner.
*/
async removeNotStarted(
actorId: string,
propertyId: string,
tenancyId: string,
today: IsoDate,
): Promise<boolean> {
const [removed] = await atomically(db, [
db
.delete(tenancies)
.where(
and(
ownTenancy(actorId, propertyId, tenancyId),
gt(tenancies.validFrom, today),
notExists(
db
.select({ one: sql`1` })
.from(rentPayments)
.where(eq(rentPayments.tenancyId, tenancies.id)),
),
),
)
.returning({ id: tenancies.id }),
])
return removed.length > 0
},
/**
* Cancels an ending: the last day goes back to the fixed term's end, or
* none. Nothing changes if that would overlap a later tenancy.
*/
async cancelEnding(
actorId: string,
propertyId: string,
tenancy: Pick<Tenancy, 'id' | 'validFrom' | 'termEnd'>,
now: Date,
): Promise<boolean> {
const [updated] = await atomically(db, [
db
.update(tenancies)
.set({
validTo: sql`${tenancies.termEnd}`,
noticeGivenBy: null,
noticeDate: null,
updatedAt: now,
})
.where(
and(
ownTenancy(actorId, propertyId, tenancy.id),
isNotNull(tenancies.noticeGivenBy),
noOverlap(
propertyId,
tenancy.validFrom,
tenancy.termEnd,
tenancy.id,
),
),
)
.returning({ id: tenancies.id }),
])
return updated.length > 0
},
}
}
export type TenancyRepository = ReturnType<typeof tenancyRepository>
|