create-protobase 0.53.5 → 0.53.7

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.
Files changed (104) hide show
  1. package/dist/templates/erp/package.json +10 -7
  2. package/dist/templates/property-portal/.env.example +18 -0
  3. package/dist/templates/property-portal/API.md +66 -0
  4. package/dist/templates/property-portal/_.gitignore +4 -0
  5. package/dist/templates/property-portal/_bunfig.toml +4 -0
  6. package/dist/templates/property-portal/config/agencies/data.ts +17 -0
  7. package/dist/templates/property-portal/config/agencies/ui.ts +20 -0
  8. package/dist/templates/property-portal/config/alerts/data.ts +20 -0
  9. package/dist/templates/property-portal/config/alerts/ui.ts +18 -0
  10. package/dist/templates/property-portal/config/bottom-bar.test.ts +30 -0
  11. package/dist/templates/property-portal/config/bottom-bar.ts +17 -0
  12. package/dist/templates/property-portal/config/config.test.ts +73 -0
  13. package/dist/templates/property-portal/config/favorites/data.ts +13 -0
  14. package/dist/templates/property-portal/config/favorites/ui.ts +26 -0
  15. package/dist/templates/property-portal/config/home/page.tsx +19 -0
  16. package/dist/templates/property-portal/config/index.ts +18 -0
  17. package/dist/templates/property-portal/config/listing_photos/data.ts +15 -0
  18. package/dist/templates/property-portal/config/listing_photos/ui.ts +20 -0
  19. package/dist/templates/property-portal/config/listings/data.ts +55 -0
  20. package/dist/templates/property-portal/config/listings/labels.ts +40 -0
  21. package/dist/templates/property-portal/config/listings/rules.test.ts +27 -0
  22. package/dist/templates/property-portal/config/listings/rules.ts +25 -0
  23. package/dist/templates/property-portal/config/listings/ui.ts +121 -0
  24. package/dist/templates/property-portal/config/map-layers.test.ts +16 -0
  25. package/dist/templates/property-portal/config/notes/data.ts +19 -0
  26. package/dist/templates/property-portal/config/notes/ui.ts +16 -0
  27. package/dist/templates/property-portal/config/photo-variants.ts +10 -0
  28. package/dist/templates/property-portal/config/places/data.ts +15 -0
  29. package/dist/templates/property-portal/config/places/ui.ts +23 -0
  30. package/dist/templates/property-portal/config/roles.test.ts +89 -0
  31. package/dist/templates/property-portal/config/roles.ts +40 -0
  32. package/dist/templates/property-portal/config/saved_searches/data.ts +25 -0
  33. package/dist/templates/property-portal/config/saved_searches/filter.ts +20 -0
  34. package/dist/templates/property-portal/config/saved_searches/rules.test.ts +14 -0
  35. package/dist/templates/property-portal/config/saved_searches/rules.ts +10 -0
  36. package/dist/templates/property-portal/config/saved_searches/ui.ts +32 -0
  37. package/dist/templates/property-portal/db/connection.ts +7 -0
  38. package/dist/templates/property-portal/db/drop.ts +15 -0
  39. package/dist/templates/property-portal/db/kysely.ts +13 -0
  40. package/dist/templates/property-portal/db/migrate.ts +31 -0
  41. package/dist/templates/property-portal/db/migrations/001_market.sql +101 -0
  42. package/dist/templates/property-portal/db/migrations/002_auth.sql +36 -0
  43. package/dist/templates/property-portal/db/migrations/003_jobs.sql +576 -0
  44. package/dist/templates/property-portal/db/migrations/004_members.sql +71 -0
  45. package/dist/templates/property-portal/db/migrations/005_listing_published.sql +14 -0
  46. package/dist/templates/property-portal/db/migrations/006_price_split.sql +61 -0
  47. package/dist/templates/property-portal/db/migrations/007_listing_position.sql +2 -0
  48. package/dist/templates/property-portal/db/migrations/007_photo_placeholders.sql +4 -0
  49. package/dist/templates/property-portal/db/migrations/008_saved_searches.sql +42 -0
  50. package/dist/templates/property-portal/db/migrations/009_listing_position_exact.sql +3 -0
  51. package/dist/templates/property-portal/db/migrations/010_saved_search_name.sql +3 -0
  52. package/dist/templates/property-portal/db/migrations/011_filter_indexes.sql +36 -0
  53. package/dist/templates/property-portal/db/migrations/012_sort_indexes.sql +4 -0
  54. package/dist/templates/property-portal/db/migrations/013_alert_mail.sql +26 -0
  55. package/dist/templates/property-portal/db/migrations/014_auth.sql +4 -0
  56. package/dist/templates/property-portal/db/migrations/015_auth.sql +319 -0
  57. package/dist/templates/property-portal/emails/alerted-listing.tsx +63 -0
  58. package/dist/templates/property-portal/emails/daily-digest.tsx +26 -0
  59. package/dist/templates/property-portal/emails/emails.test.ts +53 -0
  60. package/dist/templates/property-portal/emails/instant-alert.tsx +26 -0
  61. package/dist/templates/property-portal/jobs/alerted-listings.test.ts +19 -0
  62. package/dist/templates/property-portal/jobs/alerted-listings.ts +42 -0
  63. package/dist/templates/property-portal/jobs/daily-alerts.ts +21 -0
  64. package/dist/templates/property-portal/jobs/email-alerts.ts +44 -0
  65. package/dist/templates/property-portal/jobs/match-listing.ts +30 -0
  66. package/dist/templates/property-portal/jobs/match.test.ts +51 -0
  67. package/dist/templates/property-portal/jobs/match.ts +15 -0
  68. package/dist/templates/property-portal/jobs/offered.test.ts +9 -0
  69. package/dist/templates/property-portal/jobs/offered.ts +12 -0
  70. package/dist/templates/property-portal/map/tiles-proxy.test.ts +78 -0
  71. package/dist/templates/property-portal/map/tiles-proxy.ts +48 -0
  72. package/dist/templates/property-portal/map/tiles.ts +16 -0
  73. package/dist/templates/property-portal/node-types.d.ts +1 -0
  74. package/dist/templates/property-portal/package.json +42 -0
  75. package/dist/templates/property-portal/protobase.config.ts +57 -0
  76. package/dist/templates/property-portal/protobase.ui.tsx +21 -0
  77. package/dist/templates/property-portal/seed/builders/agency.ts +22 -0
  78. package/dist/templates/property-portal/seed/builders/distance.test.ts +14 -0
  79. package/dist/templates/property-portal/seed/builders/distance.ts +12 -0
  80. package/dist/templates/property-portal/seed/builders/listing.ts +177 -0
  81. package/dist/templates/property-portal/seed/builders/member.ts +136 -0
  82. package/dist/templates/property-portal/seed/builders/search-filter.test.ts +17 -0
  83. package/dist/templates/property-portal/seed/builders/search-filter.ts +44 -0
  84. package/dist/templates/property-portal/seed/calendar.ts +4 -0
  85. package/dist/templates/property-portal/seed/cli.ts +50 -0
  86. package/dist/templates/property-portal/seed/copy.ts +22 -0
  87. package/dist/templates/property-portal/seed/data/names.ts +61 -0
  88. package/dist/templates/property-portal/seed/data/places.ts +54 -0
  89. package/dist/templates/property-portal/seed/format.ts +20 -0
  90. package/dist/templates/property-portal/seed/maintenance.ts +65 -0
  91. package/dist/templates/property-portal/seed/photos/canvas.ts +48 -0
  92. package/dist/templates/property-portal/seed/photos/draw.ts +202 -0
  93. package/dist/templates/property-portal/seed/photos/library.ts +44 -0
  94. package/dist/templates/property-portal/seed/photos/png.ts +34 -0
  95. package/dist/templates/property-portal/seed/rng.ts +36 -0
  96. package/dist/templates/property-portal/seed/scales.ts +9 -0
  97. package/dist/templates/property-portal/seed/steps/market.ts +54 -0
  98. package/dist/templates/property-portal/seed/steps/members.ts +41 -0
  99. package/dist/templates/property-portal/seed/world.ts +98 -0
  100. package/dist/templates/property-portal/tsconfig.json +24 -0
  101. package/dist/templates/property-portal/vitest.config.ts +4 -0
  102. package/dist/templates/real-estate/package.json +10 -7
  103. package/dist/templates/scratch/package.json +10 -7
  104. package/package.json +2 -2
@@ -0,0 +1,14 @@
1
+ -- A listing that is published, created so or a draft given its `published_at`, has the match-listing job
2
+ -- (jobs/match-listing.ts) find the search profiles it fits. In the same transaction: no publish without its job.
3
+ create function market.listing_published() returns trigger
4
+ language plpgsql as $$
5
+ begin
6
+ if new.published_at is not null and (tg_op = 'INSERT' or old.published_at is null) then
7
+ perform jobs.enqueue('match-listing', jsonb_build_object('listingId', new.id), key => 'listing:' || new.id);
8
+ end if;
9
+ return new;
10
+ end
11
+ $$;
12
+
13
+ create trigger listing_published after insert or update of published_at on market.listings
14
+ for each row execute function market.listing_published();
@@ -0,0 +1,61 @@
1
+ -- A listing has a purchase price, a rent per month, or both: in the Netherlands a home for sale is often let in the
2
+ -- meantime (leegstandswet). Whether it is for sale or for rent follows from the prices it has, so the offer type goes.
3
+ -- A listing's price moves to the purchase price for a sale and to the rent for a rental, and a search profile's price
4
+ -- range to the purchase or the rent range of what it looked for.
5
+
6
+ alter table market.listings
7
+ add column purchase_price numeric(12, 2) check (purchase_price > 0),
8
+ add column rent_price numeric(12, 2) check (rent_price > 0);
9
+
10
+ update market.listings set purchase_price = price where offer_type = 'sale';
11
+ update market.listings set rent_price = price where offer_type = 'rent';
12
+
13
+ -- The price per m² is computed from the price; dropping the offer type drops the indexes and the status check on it.
14
+ alter table market.listings
15
+ drop column price_per_m2,
16
+ drop column price,
17
+ drop column offer_type;
18
+
19
+ alter table market.listings
20
+ add column price_per_m2 numeric(10, 2) generated always as (round(purchase_price / living_area, 2)) stored,
21
+ add column for_sale boolean generated always as (purchase_price is not null) stored,
22
+ add column for_rent boolean generated always as (rent_price is not null) stored,
23
+ add constraint listings_priced check (purchase_price is not null or rent_price is not null),
24
+ -- Sold needs a purchase price, let a rent.
25
+ add constraint listings_status_offered check ((status <> 'sold' or purchase_price is not null) and (status <> 'let' or rent_price is not null));
26
+
27
+ create index listings_purchase_idx on market.listings (status, purchase_price) where purchase_price is not null;
28
+ create index listings_rent_idx on market.listings (status, rent_price) where rent_price is not null;
29
+ create index listings_place_idx on market.listings (place_id, published_at desc);
30
+ create index listings_type_idx on market.listings (property_type);
31
+
32
+ alter table members.search_profiles
33
+ add column for_sale boolean not null default false,
34
+ add column for_rent boolean not null default false,
35
+ add column min_purchase_price numeric(12, 2) check (min_purchase_price >= 0),
36
+ add column max_purchase_price numeric(12, 2) check (max_purchase_price >= 0),
37
+ add column min_rent_price numeric(12, 2) check (min_rent_price >= 0),
38
+ add column max_rent_price numeric(12, 2) check (max_rent_price >= 0);
39
+
40
+ update members.search_profiles
41
+ set for_sale = true, min_purchase_price = min_price, max_purchase_price = max_price
42
+ where offer_type = 'sale';
43
+
44
+ update members.search_profiles
45
+ set for_rent = true, min_rent_price = min_price, max_rent_price = max_price
46
+ where offer_type = 'rent';
47
+
48
+ -- Dropping these drops the matching index and the range check on them.
49
+ alter table members.search_profiles
50
+ drop column offer_type,
51
+ drop column min_price,
52
+ drop column max_price;
53
+
54
+ alter table members.search_profiles
55
+ add constraint search_profiles_offer check (for_sale or for_rent),
56
+ add constraint search_profiles_purchase_range check (min_purchase_price is null or max_purchase_price is null or max_purchase_price >= min_purchase_price),
57
+ add constraint search_profiles_rent_range check (min_rent_price is null or max_rent_price is null or max_rent_price >= min_rent_price);
58
+
59
+ create index search_profiles_matching_idx on members.search_profiles (for_sale, for_rent) where active;
60
+
61
+ drop type market.offer_type;
@@ -0,0 +1,2 @@
1
+ -- The listings' map asks for those inside its view: a bounding box on the position, which this index answers.
2
+ create index listings_position_idx on market.listings (latitude, longitude);
@@ -0,0 +1,4 @@
1
+ -- Each photo's ThumbHash, which the app draws while the photo loads: the upload computes it (`.placeholder()`), and
2
+ -- photos stored before stay without one.
3
+ alter table market.listings add column photo_placeholder text;
4
+ alter table market.listing_photos add column photo_placeholder text;
@@ -0,0 +1,42 @@
1
+ -- Members save the listings page's filters as searches instead of filling in search profiles: a search is the filter
2
+ -- the page had, and whether they hear of new listings that fit it at once, in a daily digest or not at all. The
3
+ -- profiles go with their alerts, which the searches record again from the next listing published.
4
+ drop table members.alerts;
5
+ drop table members.search_profiles;
6
+ drop type members.alert_frequency;
7
+
8
+ create type members.alert_frequency as enum ('instant', 'daily', 'none');
9
+
10
+ create table members.saved_searches (
11
+ id bigint generated by default as identity primary key,
12
+ user_id text not null references auth."user" (id) on delete cascade,
13
+ name text not null check (length(trim(name)) > 0),
14
+ -- The listings' filter as the listings page's address holds it (AIP-160), such as
15
+ -- `for_sale = true AND place_id = 1 AND purchase_price >= 300000 AND purchase_price <= 500000`.
16
+ filter text not null,
17
+ alert_frequency members.alert_frequency not null default 'daily',
18
+ -- When the member last opened the search's results: the listings published since are new to them.
19
+ seen_at timestamptz not null default now(),
20
+ created_at timestamptz not null default now(),
21
+ -- The same filter is one search.
22
+ unique (user_id, filter),
23
+ -- The alerts' owner is the search's: they reference both.
24
+ unique (id, user_id)
25
+ );
26
+
27
+ create index saved_searches_user_idx on members.saved_searches (user_id, created_at desc);
28
+
29
+ create table members.alerts (
30
+ id bigint generated by default as identity primary key,
31
+ user_id text not null,
32
+ saved_search_id bigint not null,
33
+ listing_id bigint not null references market.listings (id) on delete cascade,
34
+ matched_at timestamptz not null default now(),
35
+ emailed_at timestamptz,
36
+ foreign key (saved_search_id, user_id) references members.saved_searches (id, user_id) on delete cascade,
37
+ unique (saved_search_id, listing_id)
38
+ );
39
+
40
+ create index alerts_user_idx on members.alerts (user_id, matched_at desc);
41
+ create index alerts_listing_idx on members.alerts (listing_id);
42
+ create index alerts_unmailed_idx on members.alerts (user_id) where emailed_at is null;
@@ -0,0 +1,3 @@
1
+ -- A listing may give its position only roughly, as many Spanish portals do to keep the home's door private: false
2
+ -- here, and the listings' map draws its marker as approximate.
3
+ alter table market.listings add column position_exact boolean not null default true;
@@ -0,0 +1,3 @@
1
+ -- A saved search's name is the one the member gives it when saving, or none: the searches page then titles it with its
2
+ -- filters. Names stored before were made from the filters and stay as they are.
3
+ alter table members.saved_searches alter column name drop not null;
@@ -0,0 +1,36 @@
1
+ -- A filter, sort or search reads only columns an index serves. The home page's rows of listings on offer read the
2
+ -- first two, newest first, however many listings are on offer.
3
+ create index listings_for_sale_idx on market.listings (for_sale, status, published_at desc);
4
+ create index listings_for_rent_idx on market.listings (for_rent, status, published_at desc);
5
+ create index listings_status_idx on market.listings (status, published_at desc);
6
+ create index listings_purchase_price_idx on market.listings (purchase_price);
7
+ create index listings_rent_price_idx on market.listings (rent_price);
8
+ create index listings_price_per_m2_idx on market.listings (price_per_m2);
9
+ create index listings_bedrooms_idx on market.listings (bedrooms);
10
+ create index listings_plot_area_idx on market.listings (plot_area);
11
+ create index listings_built_year_idx on market.listings (built_year);
12
+ create index listings_longitude_idx on market.listings (longitude);
13
+ create index listings_garden_idx on market.listings (garden);
14
+ create index listings_balcony_idx on market.listings (balcony);
15
+ create index listings_parking_idx on market.listings (parking);
16
+ create index listings_created_at_idx on market.listings (created_at);
17
+ create index listings_postal_code_trgm_idx on market.listings using gin (postal_code gin_trgm_ops);
18
+
19
+ create index places_region_only_idx on market.places (region);
20
+ create index places_name_idx on market.places (name);
21
+ create index places_region_trgm_idx on market.places using gin (region gin_trgm_ops);
22
+ create index agencies_name_idx on market.agencies (name);
23
+ create index agencies_created_at_idx on market.agencies (created_at);
24
+
25
+ create index alerts_saved_search_idx on members.alerts (saved_search_id, matched_at desc);
26
+ create index alerts_matched_at_idx on members.alerts (matched_at);
27
+ create index alerts_emailed_at_idx on members.alerts (emailed_at);
28
+ create index favorites_created_at_idx on members.favorites (created_at);
29
+ create index notes_listing_updated_idx on members.notes (listing_id, updated_at desc);
30
+ create index notes_created_at_idx on members.notes (created_at);
31
+ create index notes_updated_at_idx on members.notes (updated_at);
32
+ create index notes_body_trgm_idx on members.notes using gin (body gin_trgm_ops);
33
+ create index saved_searches_created_at_idx on members.saved_searches (created_at);
34
+ create index saved_searches_seen_at_idx on members.saved_searches (seen_at);
35
+ create index saved_searches_alert_frequency_idx on members.saved_searches (alert_frequency);
36
+ create index saved_searches_name_trgm_idx on members.saved_searches using gin (name gin_trgm_ops);
@@ -0,0 +1,4 @@
1
+ -- Each sortable column is served alone too, as a list sorted by it is: by address, a photo's position or a search's name.
2
+ create index listings_address_idx on market.listings (address);
3
+ create index listing_photos_position_idx on market.listing_photos (position);
4
+ create index saved_searches_name_idx on members.saved_searches (name);
@@ -0,0 +1,26 @@
1
+ -- An alert is mailed only when its search sent email as the listing fit it: `mail_due` records that, written by the
2
+ -- match-listing job (jobs/match-listing.ts). A listing found while its search sent no email is never mailed later, when
3
+ -- the member turns its email on. The alerts recorded before this migration that never went out are not due: no mail
4
+ -- goes out for a match from before the member chose to hear of it.
5
+ alter table members.alerts add column mail_due boolean;
6
+ update members.alerts set mail_due = emailed_at is not null;
7
+ alter table members.alerts alter column mail_due set not null;
8
+
9
+ drop index members.alerts_unmailed_idx;
10
+ create index alerts_unmailed_idx on members.alerts (user_id) where emailed_at is null and mail_due;
11
+
12
+ -- A search switched to mail at once mails the listings it found that still wait for the daily digest, now.
13
+ create function members.saved_search_instant() returns trigger
14
+ language plpgsql as $$
15
+ begin
16
+ if new.alert_frequency = 'instant' and old.alert_frequency <> 'instant' and exists (
17
+ select 1 from members.alerts a where a.saved_search_id = new.id and a.emailed_at is null and a.mail_due
18
+ ) then
19
+ perform jobs.enqueue('email-alerts', jsonb_build_object('userId', new.user_id, 'frequency', 'instant'), key => 'instant:' || new.user_id);
20
+ end if;
21
+ return new;
22
+ end
23
+ $$;
24
+
25
+ create trigger saved_search_instant after update of alert_frequency on members.saved_searches
26
+ for each row execute function members.saved_search_instant();
@@ -0,0 +1,4 @@
1
+ -- The auth schema changes this version of Protobase needs, written by `protobase auth migration`.
2
+ -- Applied with the project's other migrations; edit or remove statements the project handles differently.
3
+
4
+ alter table "auth"."user" add column if not exists "email_confirmed_at" timestamptz;
@@ -0,0 +1,319 @@
1
+ -- The auth schema changes this version of Protobase needs, written by `protobase auth migration`.
2
+ -- Applied with the project's other migrations; edit or remove statements the project handles differently.
3
+
4
+ -- The record of the auth store's migrations.
5
+ create schema if not exists "auth";
6
+
7
+ create table "auth"."auth_migrations" (
8
+ version integer primary key,
9
+ name text not null,
10
+ applied_at timestamptz default current_timestamp not null
11
+ );
12
+
13
+ -- The auth store an earlier Protobase made, without a record of its migrations, adopted: 1 core, 2 two_factor, 3 passkeys, 4 sign_in_policy, 5 staff_sign_in
14
+ -- recorded as applied, once every table and column they make is there.
15
+ do $adopt$
16
+ declare
17
+ missing text;
18
+ begin
19
+ select string_agg(expected.table_name || '.' || expected.column_name, ', ' order by expected.table_name, expected.column_name) into missing
20
+ from (values ('account', 'id', 'text'),
21
+ ('account', 'account_id', 'text'),
22
+ ('account', 'provider_id', 'text'),
23
+ ('account', 'user_id', 'text'),
24
+ ('account', 'access_token', 'text'),
25
+ ('account', 'refresh_token', 'text'),
26
+ ('account', 'id_token', 'text'),
27
+ ('account', 'access_token_expires_at', 'timestamptz'),
28
+ ('account', 'refresh_token_expires_at', 'timestamptz'),
29
+ ('account', 'scope', 'text'),
30
+ ('account', 'password', 'text'),
31
+ ('account', 'created_at', 'timestamptz'),
32
+ ('account', 'updated_at', 'timestamptz'),
33
+ ('jwks', 'id', 'text'),
34
+ ('jwks', 'public_key', 'text'),
35
+ ('jwks', 'private_key', 'text'),
36
+ ('jwks', 'created_at', 'timestamptz'),
37
+ ('jwks', 'expires_at', 'timestamptz'),
38
+ ('jwks', 'alg', 'text'),
39
+ ('jwks', 'crv', 'text'),
40
+ ('session', 'id', 'text'),
41
+ ('session', 'expires_at', 'timestamptz'),
42
+ ('session', 'token', 'text'),
43
+ ('session', 'created_at', 'timestamptz'),
44
+ ('session', 'updated_at', 'timestamptz'),
45
+ ('session', 'ip_address', 'text'),
46
+ ('session', 'user_agent', 'text'),
47
+ ('session', 'user_id', 'text'),
48
+ ('session', 'impersonated_by', 'text'),
49
+ ('session', 'active_role', 'text'),
50
+ ('user', 'id', 'text'),
51
+ ('user', 'name', 'text'),
52
+ ('user', 'email', 'text'),
53
+ ('user', 'email_verified', 'bool'),
54
+ ('user', 'image', 'text'),
55
+ ('user', 'created_at', 'timestamptz'),
56
+ ('user', 'updated_at', 'timestamptz'),
57
+ ('user', 'role', 'text'),
58
+ ('user', 'banned', 'bool'),
59
+ ('user', 'ban_reason', 'text'),
60
+ ('user', 'ban_expires', 'timestamptz'),
61
+ ('user', 'last_role', 'text'),
62
+ ('user', 'email_confirmed_at', 'timestamptz'),
63
+ ('verification', 'id', 'text'),
64
+ ('verification', 'identifier', 'text'),
65
+ ('verification', 'value', 'text'),
66
+ ('verification', 'expires_at', 'timestamptz'),
67
+ ('verification', 'created_at', 'timestamptz'),
68
+ ('verification', 'updated_at', 'timestamptz'),
69
+ ('two_factor', 'id', 'text'),
70
+ ('two_factor', 'secret', 'text'),
71
+ ('two_factor', 'backup_codes', 'text'),
72
+ ('two_factor', 'user_id', 'text'),
73
+ ('two_factor', 'verified', 'bool'),
74
+ ('two_factor', 'failed_verification_count', 'int4'),
75
+ ('two_factor', 'locked_until', 'timestamptz'),
76
+ ('user', 'two_factor_enabled', 'bool'),
77
+ ('passkey', 'id', 'text'),
78
+ ('passkey', 'name', 'text'),
79
+ ('passkey', 'public_key', 'text'),
80
+ ('passkey', 'user_id', 'text'),
81
+ ('passkey', 'credential_id', 'text'),
82
+ ('passkey', 'counter', 'int4'),
83
+ ('passkey', 'device_type', 'text'),
84
+ ('passkey', 'backed_up', 'bool'),
85
+ ('passkey', 'transports', 'text'),
86
+ ('passkey', 'created_at', 'timestamptz'),
87
+ ('passkey', 'aaguid', 'text'),
88
+ ('passkey', 'rp_id', 'text'),
89
+ ('sign_in_policy', 'id', 'text'),
90
+ ('sign_in_policy', 'password', 'text'),
91
+ ('sign_in_policy', 'email_code', 'text'),
92
+ ('sign_in_policy', 'passkey', 'text'),
93
+ ('sign_in_policy', 'two_factor', 'text'),
94
+ ('sign_in_policy', 'staff_access', 'text'),
95
+ ('sign_in_policy', 'platform_sign_in', 'text'),
96
+ ('sign_in_policy', 'created_at', 'timestamptz'),
97
+ ('sign_in_policy', 'created_by', 'text'),
98
+ ('staff_sign_in', 'id', 'text'),
99
+ ('staff_sign_in', 'user_id', 'text'),
100
+ ('staff_sign_in', 'user_email', 'text'),
101
+ ('staff_sign_in', 'staff', 'text'),
102
+ ('staff_sign_in', 'staff_name', 'text'),
103
+ ('staff_sign_in', 'staff_subject', 'text'),
104
+ ('staff_sign_in', 'issuer', 'text'),
105
+ ('staff_sign_in', 'reason', 'text'),
106
+ ('staff_sign_in', 'session_id', 'text'),
107
+ ('staff_sign_in', 'started_at', 'timestamptz'),
108
+ ('staff_sign_in', 'expires_at', 'timestamptz'),
109
+ ('staff_sign_in', 'ended_at', 'timestamptz')) as expected (table_name, column_name, udt_name)
110
+ where not exists (
111
+ select from information_schema.columns found
112
+ where found.table_schema = 'auth' and found.table_name = expected.table_name and found.column_name = expected.column_name and found.udt_name = expected.udt_name
113
+ );
114
+ if missing is not null then
115
+ raise exception 'The auth store does not match the migrations this file adopts: it lacks %, or has them as other types', missing;
116
+ end if;
117
+ end
118
+ $adopt$;
119
+
120
+ insert into "auth"."auth_migrations" (version, name) values (1, 'core'), (2, 'two_factor'), (3, 'passkeys'), (4, 'sign_in_policy'), (5, 'staff_sign_in');
121
+
122
+ -- 6 organizations: Organizations, their members and their invitations.
123
+ alter table "auth"."user" add column if not exists "last_organization_id" text;
124
+
125
+ alter table "auth"."session" add column active_organization_id text;
126
+
127
+ create table "auth"."organization" (
128
+ id text primary key,
129
+ name text not null,
130
+ slug text not null unique,
131
+ logo text,
132
+ created_at timestamptz not null,
133
+ metadata text
134
+ );
135
+
136
+ create table "auth"."member" (
137
+ id text primary key,
138
+ organization_id text not null references "auth"."organization" (id) on delete cascade,
139
+ user_id text not null references "auth"."user" (id) on delete cascade,
140
+ role text not null,
141
+ created_at timestamptz not null,
142
+ app_roles jsonb
143
+ );
144
+
145
+ create index member_organization_id_idx on "auth"."member" (organization_id);
146
+
147
+ create index member_user_id_idx on "auth"."member" (user_id);
148
+
149
+ create table "auth"."invitation" (
150
+ id text primary key,
151
+ organization_id text not null references "auth"."organization" (id) on delete cascade,
152
+ email text not null,
153
+ role text,
154
+ status text not null,
155
+ expires_at timestamptz not null,
156
+ created_at timestamptz default current_timestamp not null,
157
+ inviter_id text not null references "auth"."user" (id) on delete cascade,
158
+ app_roles jsonb
159
+ );
160
+
161
+ create index invitation_organization_id_idx on "auth"."invitation" (organization_id);
162
+
163
+ create index invitation_email_idx on "auth"."invitation" (email);
164
+
165
+ insert into "auth"."auth_migrations" (version, name) values (6, 'organizations');
166
+
167
+ -- 7 mcp: The clients, resources, tokens and consents of the OAuth provider for MCP.
168
+ create table "auth"."oauth_client" (
169
+ id text primary key,
170
+ client_id text not null unique,
171
+ client_secret text,
172
+ client_discovery_id text,
173
+ disabled boolean,
174
+ skip_consent boolean,
175
+ enable_end_session boolean,
176
+ subject_type text,
177
+ scopes jsonb,
178
+ client_credentials_scopes jsonb,
179
+ user_id text references "auth"."user" (id) on delete cascade,
180
+ created_at timestamptz,
181
+ updated_at timestamptz,
182
+ name text,
183
+ uri text,
184
+ icon text,
185
+ contacts jsonb,
186
+ tos text,
187
+ policy text,
188
+ software_id text,
189
+ software_version text,
190
+ software_statement text,
191
+ redirect_uris jsonb not null,
192
+ post_logout_redirect_uris jsonb,
193
+ backchannel_logout_uri text,
194
+ backchannel_logout_session_required boolean,
195
+ token_endpoint_auth_method text,
196
+ application_type text,
197
+ jwks text,
198
+ jwks_uri text,
199
+ grant_types jsonb,
200
+ response_types jsonb,
201
+ require_pkce boolean,
202
+ dpop_bound_access_tokens boolean,
203
+ reference_id text,
204
+ metadata jsonb
205
+ );
206
+
207
+ create index oauth_client_user_id_idx on "auth"."oauth_client" (user_id);
208
+
209
+ create table "auth"."oauth_resource" (
210
+ id text primary key,
211
+ identifier text not null unique,
212
+ name text not null,
213
+ access_token_ttl integer,
214
+ refresh_token_ttl integer,
215
+ signing_algorithm text,
216
+ signing_key_id text,
217
+ allowed_scopes jsonb,
218
+ custom_claims jsonb,
219
+ dpop_bound_access_tokens_required boolean,
220
+ disabled boolean,
221
+ created_at timestamptz,
222
+ updated_at timestamptz,
223
+ policy_version integer,
224
+ metadata jsonb
225
+ );
226
+
227
+ create table "auth"."oauth_client_resource" (
228
+ id text primary key,
229
+ client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
230
+ resource_id text not null references "auth"."oauth_resource" (identifier) on delete cascade,
231
+ metadata jsonb,
232
+ created_at timestamptz
233
+ );
234
+
235
+ create index oauth_client_resource_client_id_idx on "auth"."oauth_client_resource" (client_id);
236
+
237
+ create index oauth_client_resource_resource_id_idx on "auth"."oauth_client_resource" (resource_id);
238
+
239
+ create unique index oauth_client_resource_client_id_resource_id_uidx on "auth"."oauth_client_resource" (client_id, resource_id);
240
+
241
+ create table "auth"."oauth_refresh_token" (
242
+ id text primary key,
243
+ token text not null unique,
244
+ client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
245
+ session_id text references "auth"."session" (id) on delete set null,
246
+ user_id text not null references "auth"."user" (id) on delete cascade,
247
+ reference_id text,
248
+ authorization_code_id text,
249
+ resources jsonb,
250
+ requested_user_info_claims jsonb,
251
+ expires_at timestamptz not null,
252
+ created_at timestamptz not null,
253
+ revoked timestamptz,
254
+ rotated_at timestamptz,
255
+ rotation_replay_response text,
256
+ rotation_replay_expires_at timestamptz,
257
+ auth_time timestamptz,
258
+ confirmation jsonb,
259
+ scopes jsonb not null
260
+ );
261
+
262
+ create index oauth_refresh_token_client_id_idx on "auth"."oauth_refresh_token" (client_id);
263
+
264
+ create index oauth_refresh_token_session_id_idx on "auth"."oauth_refresh_token" (session_id);
265
+
266
+ create index oauth_refresh_token_user_id_idx on "auth"."oauth_refresh_token" (user_id);
267
+
268
+ create index oauth_refresh_token_authorization_code_id_idx on "auth"."oauth_refresh_token" (authorization_code_id);
269
+
270
+ create table "auth"."oauth_access_token" (
271
+ id text primary key,
272
+ token text not null unique,
273
+ client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
274
+ session_id text references "auth"."session" (id) on delete set null,
275
+ user_id text references "auth"."user" (id) on delete cascade,
276
+ reference_id text,
277
+ authorization_code_id text,
278
+ resources jsonb,
279
+ requested_user_info_claims jsonb,
280
+ refresh_id text references "auth"."oauth_refresh_token" (id) on delete cascade,
281
+ expires_at timestamptz not null,
282
+ created_at timestamptz not null,
283
+ revoked timestamptz,
284
+ confirmation jsonb,
285
+ scopes jsonb not null
286
+ );
287
+
288
+ create index oauth_access_token_client_id_idx on "auth"."oauth_access_token" (client_id);
289
+
290
+ create index oauth_access_token_session_id_idx on "auth"."oauth_access_token" (session_id);
291
+
292
+ create index oauth_access_token_user_id_idx on "auth"."oauth_access_token" (user_id);
293
+
294
+ create index oauth_access_token_authorization_code_id_idx on "auth"."oauth_access_token" (authorization_code_id);
295
+
296
+ create index oauth_access_token_refresh_id_idx on "auth"."oauth_access_token" (refresh_id);
297
+
298
+ create table "auth"."oauth_consent" (
299
+ id text primary key,
300
+ client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
301
+ user_id text references "auth"."user" (id) on delete cascade,
302
+ reference_id text,
303
+ resources jsonb,
304
+ requested_user_info_claims jsonb,
305
+ scopes jsonb not null,
306
+ created_at timestamptz not null,
307
+ updated_at timestamptz not null
308
+ );
309
+
310
+ create index oauth_consent_client_id_idx on "auth"."oauth_consent" (client_id);
311
+
312
+ create index oauth_consent_user_id_idx on "auth"."oauth_consent" (user_id);
313
+
314
+ create table "auth"."oauth_client_assertion" (
315
+ id text primary key,
316
+ expires_at timestamptz not null
317
+ );
318
+
319
+ insert into "auth"."auth_migrations" (version, name) values (7, 'mcp');
@@ -0,0 +1,63 @@
1
+ import { EmailNote } from '@protobase/server/email'
2
+ import { Hr, Link, Section, Text } from 'react-email'
3
+
4
+ /** A search of the member's that a listing fits: its name, `null` for one saved without a name, and its page. */
5
+ export type FittingSearch = { name: string | null; url: string }
6
+
7
+ /** A new listing as an alert mails it: a price empty when it is not offered that way, its page, and the searches it fits. */
8
+ export type AlertedListing = {
9
+ address: string
10
+ place: string
11
+ purchasePrice: string | null
12
+ rentPrice: string | null
13
+ livingArea: number
14
+ url: string
15
+ searches: FittingSearch[]
16
+ }
17
+
18
+ const euros = (amount: string) => `€${Math.round(Number(amount)).toLocaleString('en-GB')}`
19
+
20
+ /** What it costs each way it is offered: "For sale: €650,000 or for rent: €2,100 a month". */
21
+ export const prices = (listing: Pick<AlertedListing, 'purchasePrice' | 'rentPrice'>) => {
22
+ const offers = [
23
+ ...(listing.purchasePrice === null ? [] : [`for sale: ${euros(listing.purchasePrice)}`]),
24
+ ...(listing.rentPrice === null ? [] : [`for rent: ${euros(listing.rentPrice)} a month`]),
25
+ ].join(' or ')
26
+ return offers.charAt(0).toUpperCase() + offers.slice(1)
27
+ }
28
+
29
+ const searchLabel = (search: FittingSearch) => (search.name === null ? 'one of your searches' : `"${search.name}"`)
30
+
31
+ /** The searches a listing fits, each linked to its page: Fits "Canal flat" and "Under 700k". */
32
+ const Fits = ({ searches }: { searches: FittingSearch[] }) => (
33
+ <Text style={{ margin: '4px 0', color: '#71717a' }}>
34
+ Fits{' '}
35
+ {searches.map((search, index) => (
36
+ <span key={search.url}>
37
+ {index === 0 ? '' : index === searches.length - 1 ? ' and ' : ', '}
38
+ <Link href={search.url}>{searchLabel(search)}</Link>
39
+ </span>
40
+ ))}
41
+ </Text>
42
+ )
43
+
44
+ /** One listing of an alert: where it is, what it costs, its size, the searches it fits and a link to its page. */
45
+ export const ListingBlock = ({ listing }: { listing: AlertedListing }) => (
46
+ <Section>
47
+ <Hr style={{ borderColor: '#e4e4e7' }} />
48
+ <Link href={listing.url} style={{ color: '#0f766e', fontSize: '16px', fontWeight: 600 }}>
49
+ {listing.address}, {listing.place}
50
+ </Link>
51
+ <Text style={{ margin: '4px 0' }}>
52
+ {prices(listing)}, {listing.livingArea} m²
53
+ </Text>
54
+ <Fits searches={listing.searches} />
55
+ </Section>
56
+ )
57
+
58
+ /** The mail's footer: where the member changes what they hear about. */
59
+ export const SearchesFooter = ({ searchesUrl }: { searchesUrl: string }) => (
60
+ <EmailNote>
61
+ You get this mail for your saved searches. Change how often you hear of one, or stop, under <Link href={searchesUrl}>Searches</Link>.
62
+ </EmailNote>
63
+ )
@@ -0,0 +1,26 @@
1
+ import { defineEmail } from '@protobase/server'
2
+ import { EmailLayout, type MailingApp } from '@protobase/server/email'
3
+ import { Text } from 'react-email'
4
+ import { ListingBlock, SearchesFooter, type AlertedListing } from './alerted-listing'
5
+
6
+ /** The listings that fit a member's daily searches since the last digest. */
7
+ export type DailyDigest = { app: MailingApp; listings: AlertedListing[]; searchesUrl: string }
8
+
9
+ const homes = (count: number) => `${count} new ${count === 1 ? 'home fits' : 'homes fit'} your searches`
10
+
11
+ const DailyDigestEmail = ({ app, listings, searchesUrl }: DailyDigest) => (
12
+ <EmailLayout app={app} preview={homes(listings.length)} heading="Your daily digest">
13
+ <Text>These homes were published since your last digest:</Text>
14
+ {listings.map((listing) => (
15
+ <ListingBlock key={listing.url} listing={listing} />
16
+ ))}
17
+ <SearchesFooter searchesUrl={searchesUrl} />
18
+ </EmailLayout>
19
+ )
20
+
21
+ /** Every morning, the listings of the day before that fit a member's daily searches, in one mail. */
22
+ export const dailyDigest = defineEmail({
23
+ name: 'daily-digest',
24
+ subject: ({ listings }: DailyDigest) => homes(listings.length),
25
+ component: DailyDigestEmail,
26
+ })