@vrplatform/kysely 1.3.45-stage.744 → 1.3.45-stage.820

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,135 @@
1
+ import type { Kysely } from 'kysely';
2
+ import { sql } from 'kysely';
3
+
4
+ // CUS-2977: a billing partner homed in another data region has no row in this
5
+ // regional database. That absence is valid and must not make the team count
6
+ // zero active listings. Only a locally present, non-active billing partner
7
+ // does, matching kysely `whereTenantActive` (PRO-17712). `create or replace`
8
+ // keeps the existing grants.
9
+ export async function up(db: Kysely<unknown>): Promise<void> {
10
+ await sql`
11
+ create or replace function billing.active_listing_count(
12
+ selected_tenant_id uuid
13
+ ) returns integer
14
+ language sql
15
+ stable
16
+ as $$
17
+ select count(*)::integer
18
+ from public.listing as listing
19
+ inner join public.tenant as tenant
20
+ on tenant.id = listing.tenant_id
21
+ where listing.tenant_id = selected_tenant_id
22
+ and tenant.calculated_status = 'active'
23
+ and not exists (
24
+ select 1
25
+ from public.tenant as billing_partner
26
+ where billing_partner.id = coalesce(
27
+ tenant.calculated_billing_partner_id,
28
+ '7bfec41f-aa3f-4602-95d2-f7996e4f0e59'::uuid
29
+ )
30
+ and billing_partner.calculated_status is distinct from 'active'
31
+ )
32
+ and (
33
+ (
34
+ tenant.is_general_ledger = false
35
+ and listing.calculated_status = 'active'
36
+ ) or (
37
+ tenant.is_general_ledger = true
38
+ and not exists (
39
+ select 1
40
+ from public.listing_ownership_period as ownership
41
+ where ownership.listing_id = coalesce(
42
+ listing.parent_id,
43
+ listing.id
44
+ )
45
+ and ownership.tenant_id = listing.tenant_id
46
+ and ownership.set_listing_inactive = true
47
+ and ownership.start_at <= current_date
48
+ and (
49
+ ownership.end_at is null
50
+ or ownership.end_at >= current_date
51
+ )
52
+ )
53
+ )
54
+ )
55
+ $$
56
+ `.execute(db);
57
+
58
+ // Append a corrected fact, effective now, for every active team whose
59
+ // billing partner has no local row. These are the only teams whose count
60
+ // changes. Completed-month billing reads the latest fact before the month
61
+ // ends, so applying this before the month closes corrects that month.
62
+ // Earlier wrong facts stay as recorded history.
63
+ await sql`
64
+ select billing.record_listing_facts(
65
+ array(
66
+ select tenant.id
67
+ from public.tenant as tenant
68
+ where tenant.type = 'propertyManager'
69
+ and tenant.calculated_status = 'active'
70
+ and tenant.calculated_billing_partner_id is not null
71
+ and not exists (
72
+ select 1
73
+ from public.tenant as billing_partner
74
+ where billing_partner.id = tenant.calculated_billing_partner_id
75
+ )
76
+ and exists (
77
+ select 1
78
+ from billing.listing_fact as fact
79
+ where fact.tenant_id = tenant.id
80
+ )
81
+ ),
82
+ 'daily_reconciliation',
83
+ gen_random_uuid(),
84
+ now(),
85
+ true
86
+ )
87
+ `.execute(db);
88
+ }
89
+
90
+ export async function down(db: Kysely<unknown>): Promise<void> {
91
+ await sql`
92
+ create or replace function billing.active_listing_count(
93
+ selected_tenant_id uuid
94
+ ) returns integer
95
+ language sql
96
+ stable
97
+ as $$
98
+ select count(*)::integer
99
+ from public.listing as listing
100
+ inner join public.tenant as tenant
101
+ on tenant.id = listing.tenant_id
102
+ left join public.tenant as billing_partner
103
+ on billing_partner.id = tenant.calculated_billing_partner_id
104
+ where listing.tenant_id = selected_tenant_id
105
+ and tenant.calculated_status = 'active'
106
+ and (
107
+ tenant.calculated_billing_partner_id is null
108
+ or billing_partner.calculated_status = 'active'
109
+ )
110
+ and (
111
+ (
112
+ tenant.is_general_ledger = false
113
+ and listing.calculated_status = 'active'
114
+ ) or (
115
+ tenant.is_general_ledger = true
116
+ and not exists (
117
+ select 1
118
+ from public.listing_ownership_period as ownership
119
+ where ownership.listing_id = coalesce(
120
+ listing.parent_id,
121
+ listing.id
122
+ )
123
+ and ownership.tenant_id = listing.tenant_id
124
+ and ownership.set_listing_inactive = true
125
+ and ownership.start_at <= current_date
126
+ and (
127
+ ownership.end_at is null
128
+ or ownership.end_at >= current_date
129
+ )
130
+ )
131
+ )
132
+ )
133
+ $$
134
+ `.execute(db);
135
+ }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.3.45-stage.744",
6
+ "version": "1.3.45-stage.820",
7
7
  "description": "",
8
8
  "main": "build/main/index.js",
9
9
  "module": "build/module/index.js",