create-protobase 0.53.6 → 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.
- package/dist/templates/erp/package.json +10 -7
- package/dist/templates/property-portal/.env.example +18 -0
- package/dist/templates/property-portal/API.md +66 -0
- package/dist/templates/property-portal/_.gitignore +4 -0
- package/dist/templates/property-portal/_bunfig.toml +4 -0
- package/dist/templates/property-portal/config/agencies/data.ts +17 -0
- package/dist/templates/property-portal/config/agencies/ui.ts +20 -0
- package/dist/templates/property-portal/config/alerts/data.ts +20 -0
- package/dist/templates/property-portal/config/alerts/ui.ts +18 -0
- package/dist/templates/property-portal/config/bottom-bar.test.ts +30 -0
- package/dist/templates/property-portal/config/bottom-bar.ts +17 -0
- package/dist/templates/property-portal/config/config.test.ts +73 -0
- package/dist/templates/property-portal/config/favorites/data.ts +13 -0
- package/dist/templates/property-portal/config/favorites/ui.ts +26 -0
- package/dist/templates/property-portal/config/home/page.tsx +19 -0
- package/dist/templates/property-portal/config/index.ts +18 -0
- package/dist/templates/property-portal/config/listing_photos/data.ts +15 -0
- package/dist/templates/property-portal/config/listing_photos/ui.ts +20 -0
- package/dist/templates/property-portal/config/listings/data.ts +55 -0
- package/dist/templates/property-portal/config/listings/labels.ts +40 -0
- package/dist/templates/property-portal/config/listings/rules.test.ts +27 -0
- package/dist/templates/property-portal/config/listings/rules.ts +25 -0
- package/dist/templates/property-portal/config/listings/ui.ts +121 -0
- package/dist/templates/property-portal/config/map-layers.test.ts +16 -0
- package/dist/templates/property-portal/config/notes/data.ts +19 -0
- package/dist/templates/property-portal/config/notes/ui.ts +16 -0
- package/dist/templates/property-portal/config/photo-variants.ts +10 -0
- package/dist/templates/property-portal/config/places/data.ts +15 -0
- package/dist/templates/property-portal/config/places/ui.ts +23 -0
- package/dist/templates/property-portal/config/roles.test.ts +89 -0
- package/dist/templates/property-portal/config/roles.ts +40 -0
- package/dist/templates/property-portal/config/saved_searches/data.ts +25 -0
- package/dist/templates/property-portal/config/saved_searches/filter.ts +20 -0
- package/dist/templates/property-portal/config/saved_searches/rules.test.ts +14 -0
- package/dist/templates/property-portal/config/saved_searches/rules.ts +10 -0
- package/dist/templates/property-portal/config/saved_searches/ui.ts +32 -0
- package/dist/templates/property-portal/db/connection.ts +7 -0
- package/dist/templates/property-portal/db/drop.ts +15 -0
- package/dist/templates/property-portal/db/kysely.ts +13 -0
- package/dist/templates/property-portal/db/migrate.ts +31 -0
- package/dist/templates/property-portal/db/migrations/001_market.sql +101 -0
- package/dist/templates/property-portal/db/migrations/002_auth.sql +36 -0
- package/dist/templates/property-portal/db/migrations/003_jobs.sql +576 -0
- package/dist/templates/property-portal/db/migrations/004_members.sql +71 -0
- package/dist/templates/property-portal/db/migrations/005_listing_published.sql +14 -0
- package/dist/templates/property-portal/db/migrations/006_price_split.sql +61 -0
- package/dist/templates/property-portal/db/migrations/007_listing_position.sql +2 -0
- package/dist/templates/property-portal/db/migrations/007_photo_placeholders.sql +4 -0
- package/dist/templates/property-portal/db/migrations/008_saved_searches.sql +42 -0
- package/dist/templates/property-portal/db/migrations/009_listing_position_exact.sql +3 -0
- package/dist/templates/property-portal/db/migrations/010_saved_search_name.sql +3 -0
- package/dist/templates/property-portal/db/migrations/011_filter_indexes.sql +36 -0
- package/dist/templates/property-portal/db/migrations/012_sort_indexes.sql +4 -0
- package/dist/templates/property-portal/db/migrations/013_alert_mail.sql +26 -0
- package/dist/templates/property-portal/db/migrations/014_auth.sql +4 -0
- package/dist/templates/property-portal/db/migrations/015_auth.sql +319 -0
- package/dist/templates/property-portal/emails/alerted-listing.tsx +63 -0
- package/dist/templates/property-portal/emails/daily-digest.tsx +26 -0
- package/dist/templates/property-portal/emails/emails.test.ts +53 -0
- package/dist/templates/property-portal/emails/instant-alert.tsx +26 -0
- package/dist/templates/property-portal/jobs/alerted-listings.test.ts +19 -0
- package/dist/templates/property-portal/jobs/alerted-listings.ts +42 -0
- package/dist/templates/property-portal/jobs/daily-alerts.ts +21 -0
- package/dist/templates/property-portal/jobs/email-alerts.ts +44 -0
- package/dist/templates/property-portal/jobs/match-listing.ts +30 -0
- package/dist/templates/property-portal/jobs/match.test.ts +51 -0
- package/dist/templates/property-portal/jobs/match.ts +15 -0
- package/dist/templates/property-portal/jobs/offered.test.ts +9 -0
- package/dist/templates/property-portal/jobs/offered.ts +12 -0
- package/dist/templates/property-portal/map/tiles-proxy.test.ts +78 -0
- package/dist/templates/property-portal/map/tiles-proxy.ts +48 -0
- package/dist/templates/property-portal/map/tiles.ts +16 -0
- package/dist/templates/property-portal/node-types.d.ts +1 -0
- package/dist/templates/property-portal/package.json +42 -0
- package/dist/templates/property-portal/protobase.config.ts +57 -0
- package/dist/templates/property-portal/protobase.ui.tsx +21 -0
- package/dist/templates/property-portal/seed/builders/agency.ts +22 -0
- package/dist/templates/property-portal/seed/builders/distance.test.ts +14 -0
- package/dist/templates/property-portal/seed/builders/distance.ts +12 -0
- package/dist/templates/property-portal/seed/builders/listing.ts +177 -0
- package/dist/templates/property-portal/seed/builders/member.ts +136 -0
- package/dist/templates/property-portal/seed/builders/search-filter.test.ts +17 -0
- package/dist/templates/property-portal/seed/builders/search-filter.ts +44 -0
- package/dist/templates/property-portal/seed/calendar.ts +4 -0
- package/dist/templates/property-portal/seed/cli.ts +50 -0
- package/dist/templates/property-portal/seed/copy.ts +22 -0
- package/dist/templates/property-portal/seed/data/names.ts +61 -0
- package/dist/templates/property-portal/seed/data/places.ts +54 -0
- package/dist/templates/property-portal/seed/format.ts +20 -0
- package/dist/templates/property-portal/seed/maintenance.ts +65 -0
- package/dist/templates/property-portal/seed/photos/canvas.ts +48 -0
- package/dist/templates/property-portal/seed/photos/draw.ts +202 -0
- package/dist/templates/property-portal/seed/photos/library.ts +44 -0
- package/dist/templates/property-portal/seed/photos/png.ts +34 -0
- package/dist/templates/property-portal/seed/rng.ts +36 -0
- package/dist/templates/property-portal/seed/scales.ts +9 -0
- package/dist/templates/property-portal/seed/steps/market.ts +54 -0
- package/dist/templates/property-portal/seed/steps/members.ts +41 -0
- package/dist/templates/property-portal/seed/world.ts +98 -0
- package/dist/templates/property-portal/tsconfig.json +24 -0
- package/dist/templates/property-portal/vitest.config.ts +4 -0
- package/dist/templates/real-estate/package.json +10 -7
- package/dist/templates/scratch/package.json +10 -7
- package/package.json +2 -2
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Db } from '../db/connection'
|
|
2
|
+
|
|
3
|
+
const schemas = ['market', 'members']
|
|
4
|
+
|
|
5
|
+
// The app's tables, and the accounts an earlier seed made (ids starting with `seed-`); accounts made with
|
|
6
|
+
// `protobase users create` stay.
|
|
7
|
+
export const truncateAll = async (sql: Db) => {
|
|
8
|
+
const tables = await sql`
|
|
9
|
+
select format('%I.%I', schemaname, tablename) as name from pg_tables
|
|
10
|
+
where schemaname in ${sql(schemas)} or tablename = 'seed_info'
|
|
11
|
+
`
|
|
12
|
+
await sql.unsafe(`truncate ${tables.map((table) => table.name).join(', ')} restart identity cascade`)
|
|
13
|
+
await sql`delete from auth."user" where id like 'seed-%'`
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Secondary indexes only: primary keys and unique constraints stay in place.
|
|
17
|
+
const secondaryIndexes = (sql: Db) => sql`
|
|
18
|
+
select format('%I.%I', n.nspname, c.relname) as name, pg_get_indexdef(i.indexrelid) as definition
|
|
19
|
+
from pg_index i
|
|
20
|
+
join pg_class c on c.oid = i.indexrelid
|
|
21
|
+
join pg_namespace n on n.oid = c.relnamespace
|
|
22
|
+
where n.nspname in ${sql(schemas)}
|
|
23
|
+
and not i.indisprimary
|
|
24
|
+
and not exists (select 1 from pg_constraint k where k.conindid = i.indexrelid)
|
|
25
|
+
`
|
|
26
|
+
|
|
27
|
+
// Loading into indexed tables is several times slower than building the indexes afterwards.
|
|
28
|
+
export const dropIndexes = async (sql: Db) => {
|
|
29
|
+
const indexes = await secondaryIndexes(sql)
|
|
30
|
+
for (const index of indexes) await sql.unsafe(`drop index ${index.name}`)
|
|
31
|
+
return indexes.map((index) => index.definition as string)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const createIndexes = async (sql: Db, definitions: string[]) => {
|
|
35
|
+
await Promise.all(definitions.map((definition) => sql.unsafe(definition)))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// The trigger that has a published listing matched against the saved searches: the seed writes the alerts itself, as
|
|
39
|
+
// the job would have recorded them. A table's owner may switch its own triggers off, unlike the foreign key checks.
|
|
40
|
+
export const setTriggers = async (sql: Db, enabled: boolean) => {
|
|
41
|
+
const tables = await sql`
|
|
42
|
+
select distinct format('%I.%I', n.nspname, c.relname) as name
|
|
43
|
+
from pg_trigger t
|
|
44
|
+
join pg_class c on c.oid = t.tgrelid
|
|
45
|
+
join pg_namespace n on n.oid = c.relnamespace
|
|
46
|
+
where not t.tgisinternal and n.nspname in ${sql(schemas)}
|
|
47
|
+
`
|
|
48
|
+
for (const table of tables) await sql.unsafe(`alter table ${table.name} ${enabled ? 'enable' : 'disable'} trigger user`)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// COPY with explicit ids leaves identity sequences at 1.
|
|
52
|
+
export const resetSequences = async (sql: Db) => {
|
|
53
|
+
const columns = await sql`
|
|
54
|
+
select table_schema as schema, table_name as table, column_name as column from information_schema.columns
|
|
55
|
+
where is_identity = 'YES' and table_schema in ${sql(schemas)}
|
|
56
|
+
`
|
|
57
|
+
for (const { schema, table, column } of columns) {
|
|
58
|
+
const target = `${schema}.${table}`
|
|
59
|
+
await sql.unsafe(`select setval(pg_get_serial_sequence('${target}', '${column}'), coalesce(max(${column}), 1), max(${column}) is not null) from ${target}`)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export const analyzeAll = async (sql: Db) => {
|
|
64
|
+
await sql.unsafe('analyze')
|
|
65
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export type Rgb = readonly [number, number, number]
|
|
2
|
+
|
|
3
|
+
/** A plain raster to draw the seed's pictures on: rectangles, polygons and circles in solid colours. */
|
|
4
|
+
export const createCanvas = (width: number, height: number) => {
|
|
5
|
+
const pixels = new Uint8Array(width * height * 3)
|
|
6
|
+
const set = (x: number, y: number, [r, g, b]: Rgb) => {
|
|
7
|
+
if (x < 0 || y < 0 || x >= width || y >= height) return
|
|
8
|
+
const at = (y * width + x) * 3
|
|
9
|
+
pixels[at] = r
|
|
10
|
+
pixels[at + 1] = g
|
|
11
|
+
pixels[at + 2] = b
|
|
12
|
+
}
|
|
13
|
+
const rect = (x: number, y: number, w: number, h: number, color: Rgb) => {
|
|
14
|
+
for (let row = Math.max(0, Math.round(y)); row < Math.min(height, Math.round(y + h)); row++) {
|
|
15
|
+
for (let col = Math.max(0, Math.round(x)); col < Math.min(width, Math.round(x + w)); col++) set(col, row, color)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
// Even-odd scanline fill.
|
|
19
|
+
const polygon = (points: ReadonlyArray<readonly [number, number]>, color: Rgb) => {
|
|
20
|
+
const ys = points.map(([, y]) => y)
|
|
21
|
+
for (let y = Math.max(0, Math.floor(Math.min(...ys))); y <= Math.min(height - 1, Math.ceil(Math.max(...ys))); y++) {
|
|
22
|
+
const crossings: number[] = []
|
|
23
|
+
for (let i = 0; i < points.length; i++) {
|
|
24
|
+
const [x1, y1] = points[i]!
|
|
25
|
+
const [x2, y2] = points[(i + 1) % points.length]!
|
|
26
|
+
if ((y1 <= y && y2 > y) || (y2 <= y && y1 > y)) crossings.push(x1 + ((y - y1) / (y2 - y1)) * (x2 - x1))
|
|
27
|
+
}
|
|
28
|
+
crossings.sort((a, b) => a - b)
|
|
29
|
+
for (let i = 0; i + 1 < crossings.length; i += 2) rect(crossings[i]!, y, crossings[i + 1]! - crossings[i]!, 1, color)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const circle = (cx: number, cy: number, radius: number, color: Rgb) => {
|
|
33
|
+
for (let y = Math.floor(cy - radius); y <= cy + radius; y++) {
|
|
34
|
+
const half = Math.sqrt(Math.max(0, radius * radius - (y - cy) ** 2))
|
|
35
|
+
rect(cx - half, y, half * 2, 1, color)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// Top to bottom, from one colour to the other.
|
|
39
|
+
const gradient = (y: number, h: number, from: Rgb, to: Rgb) => {
|
|
40
|
+
for (let row = 0; row < h; row++) {
|
|
41
|
+
const t = row / Math.max(1, h - 1)
|
|
42
|
+
rect(0, y + row, width, 1, [0, 1, 2].map((i) => Math.round(from[i]! + (to[i]! - from[i]!) * t)) as unknown as Rgb)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return { width, height, pixels, rect, polygon, circle, gradient }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type Canvas = ReturnType<typeof createCanvas>
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { createCanvas, type Canvas, type Rgb } from './canvas'
|
|
2
|
+
import { pick, rngAt, type Rand } from '../rng'
|
|
3
|
+
|
|
4
|
+
export const photoWidth = 640
|
|
5
|
+
export const photoHeight = 480
|
|
6
|
+
|
|
7
|
+
const bricks: Rgb[] = [[150, 72, 52], [120, 60, 46], [176, 98, 66], [98, 52, 44], [160, 120, 90]]
|
|
8
|
+
const plaster: Rgb[] = [[244, 238, 226], [236, 220, 196], [250, 246, 240], [226, 206, 170], [214, 222, 226]]
|
|
9
|
+
const roofs: Rgb[] = [[70, 62, 60], [128, 58, 42], [96, 96, 104], [150, 76, 50]]
|
|
10
|
+
const doors: Rgb[] = [[34, 70, 54], [30, 50, 96], [120, 30, 34], [60, 40, 30]]
|
|
11
|
+
const glass: Rgb = [176, 206, 226]
|
|
12
|
+
const frame: Rgb = [250, 250, 248]
|
|
13
|
+
|
|
14
|
+
const sky = (c: Canvas, rand: Rand) => {
|
|
15
|
+
const sunny = rand() < 0.6
|
|
16
|
+
c.gradient(0, 340, sunny ? [92, 156, 222] : [150, 170, 190], sunny ? [196, 224, 246] : [214, 222, 230])
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const ground = (c: Canvas, color: Rgb) => c.rect(0, 340, c.width, 140, color)
|
|
20
|
+
|
|
21
|
+
const window = (c: Canvas, x: number, y: number, w: number, h: number) => {
|
|
22
|
+
c.rect(x - 3, y - 3, w + 6, h + 6, frame)
|
|
23
|
+
c.rect(x, y, w, h, glass)
|
|
24
|
+
c.rect(x + w / 2 - 1, y, 2, h, frame)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const tree = (c: Canvas, x: number, rand: Rand) => {
|
|
28
|
+
c.rect(x - 5, 260, 10, 85, [92, 64, 40])
|
|
29
|
+
const leaves: Rgb = pick(rand, [[64, 120, 60], [84, 140, 64], [52, 104, 70]])
|
|
30
|
+
c.circle(x, 240, 38, leaves)
|
|
31
|
+
c.circle(x - 24, 262, 26, leaves)
|
|
32
|
+
c.circle(x + 24, 262, 26, leaves)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// A tall block of flats, its windows in a grid.
|
|
36
|
+
const apartment = (c: Canvas, rand: Rand) => {
|
|
37
|
+
const wall = pick(rand, [...bricks, ...plaster])
|
|
38
|
+
const floors = 4 + Math.floor(rand() * 3)
|
|
39
|
+
const top = 340 - floors * 52
|
|
40
|
+
c.rect(130, top, 380, floors * 52, wall)
|
|
41
|
+
for (let floor = 0; floor < floors; floor++) {
|
|
42
|
+
for (let col = 0; col < 5; col++) window(c, 152 + col * 72, top + 12 + floor * 52, 44, 30)
|
|
43
|
+
if (rand() < 0.5) c.rect(140, top + 46 + floor * 52, 360, 4, [80, 80, 84])
|
|
44
|
+
}
|
|
45
|
+
c.rect(298, 300, 44, 40, pick(rand, doors))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// A house with a pitched roof, a door and windows, and a tree beside it.
|
|
49
|
+
const house = (c: Canvas, rand: Rand) => {
|
|
50
|
+
const wall = pick(rand, [...bricks, ...plaster])
|
|
51
|
+
c.rect(170, 210, 300, 130, wall)
|
|
52
|
+
c.polygon([[150, 212], [320, 110], [490, 212]], pick(rand, roofs))
|
|
53
|
+
window(c, 200, 240, 70, 50)
|
|
54
|
+
window(c, 370, 240, 70, 50)
|
|
55
|
+
window(c, 296, 150, 48, 38)
|
|
56
|
+
c.rect(298, 280, 44, 60, pick(rand, doors))
|
|
57
|
+
tree(c, 560, rand)
|
|
58
|
+
if (rand() < 0.5) tree(c, 80, rand)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// A row of narrow Dutch houses with stepped and bell gables.
|
|
62
|
+
const townhouse = (c: Canvas, rand: Rand) => {
|
|
63
|
+
for (let i = 0; i < 4; i++) {
|
|
64
|
+
const x = 60 + i * 130
|
|
65
|
+
const height = 190 + Math.floor(rand() * 40)
|
|
66
|
+
const top = 340 - height
|
|
67
|
+
const wall = pick(rand, bricks)
|
|
68
|
+
c.rect(x, top, 124, height, wall)
|
|
69
|
+
if (rand() < 0.5) {
|
|
70
|
+
c.polygon([[x, top + 1], [x + 20, top + 1], [x + 20, top - 18], [x + 42, top - 18], [x + 42, top - 36], [x + 82, top - 36], [x + 82, top - 18], [x + 104, top - 18], [x + 104, top + 1], [x + 124, top + 1]], wall)
|
|
71
|
+
} else {
|
|
72
|
+
c.polygon([[x + 10, top + 1], [x + 40, top - 30], [x + 84, top - 30], [x + 114, top + 1]], wall)
|
|
73
|
+
c.rect(x + 40, top - 34, 44, 6, frame)
|
|
74
|
+
}
|
|
75
|
+
for (let floor = 0; floor < Math.floor(height / 60); floor++) {
|
|
76
|
+
window(c, x + 18, top + 16 + floor * 58, 36, 38)
|
|
77
|
+
window(c, x + 70, top + 16 + floor * 58, 36, 38)
|
|
78
|
+
}
|
|
79
|
+
c.rect(x + 44, 300, 36, 40, pick(rand, doors))
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// A wide white villa with a flat roof, large windows and a pool in front.
|
|
84
|
+
const villa = (c: Canvas, rand: Rand) => {
|
|
85
|
+
const wall = pick(rand, plaster)
|
|
86
|
+
c.rect(90, 220, 460, 120, wall)
|
|
87
|
+
c.rect(250, 160, 220, 62, wall)
|
|
88
|
+
c.rect(80, 214, 480, 8, [200, 196, 190])
|
|
89
|
+
window(c, 120, 250, 110, 70)
|
|
90
|
+
window(c, 400, 250, 120, 70)
|
|
91
|
+
window(c, 280, 176, 160, 34)
|
|
92
|
+
c.rect(270, 270, 70, 70, [120, 90, 60])
|
|
93
|
+
c.rect(140, 380, 360, 60, [64, 164, 214])
|
|
94
|
+
c.rect(140, 380, 360, 6, [236, 236, 230])
|
|
95
|
+
if (rand() < 0.7) {
|
|
96
|
+
c.rect(588, 230, 8, 112, [120, 92, 60])
|
|
97
|
+
c.polygon([[592, 230], [552, 250], [592, 238], [632, 250]], [70, 130, 70])
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// A small building with one large window: a studio.
|
|
102
|
+
const studio = (c: Canvas, rand: Rand) => {
|
|
103
|
+
c.rect(200, 180, 240, 160, pick(rand, [...plaster, ...bricks]))
|
|
104
|
+
window(c, 236, 214, 168, 80)
|
|
105
|
+
c.rect(300, 300, 40, 40, pick(rand, doors))
|
|
106
|
+
tree(c, 520, rand)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// A building whose top floor is a glass penthouse with a terrace.
|
|
110
|
+
const penthouse = (c: Canvas, rand: Rand) => {
|
|
111
|
+
apartment(c, rand)
|
|
112
|
+
c.rect(160, 92, 320, 4, [70, 70, 74])
|
|
113
|
+
c.rect(210, 40, 220, 52, glass)
|
|
114
|
+
for (let x = 210; x <= 430; x += 44) c.rect(x, 40, 3, 52, frame)
|
|
115
|
+
for (let x = 160; x <= 480; x += 16) c.rect(x, 78, 2, 14, [70, 70, 74])
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const exteriors = { apartment, house, townhouse, villa, studio, penthouse }
|
|
119
|
+
|
|
120
|
+
export type PropertyType = keyof typeof exteriors
|
|
121
|
+
|
|
122
|
+
/** The outside of a home of a kind, one of a few variants per kind. */
|
|
123
|
+
export const drawExterior = (type: PropertyType, variant: number) => {
|
|
124
|
+
const c = createCanvas(photoWidth, photoHeight)
|
|
125
|
+
const rand = rngAt(9100 + Object.keys(exteriors).indexOf(type), variant)
|
|
126
|
+
sky(c, rand)
|
|
127
|
+
ground(c, type === 'villa' ? [214, 196, 160] : pick(rand, [[96, 150, 80], [120, 160, 90], [150, 150, 150]]))
|
|
128
|
+
exteriors[type](c, rand)
|
|
129
|
+
return c
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const floorColors: Rgb[] = [[176, 136, 96], [196, 170, 140], [140, 104, 76], [214, 210, 204]]
|
|
133
|
+
const wallColors: Rgb[] = [[240, 236, 228], [226, 232, 236], [238, 226, 212], [220, 228, 216]]
|
|
134
|
+
|
|
135
|
+
const room = (c: Canvas, rand: Rand) => {
|
|
136
|
+
c.rect(0, 0, c.width, 330, pick(rand, wallColors))
|
|
137
|
+
c.rect(0, 330, c.width, 150, pick(rand, floorColors))
|
|
138
|
+
c.rect(0, 326, c.width, 6, [250, 250, 250])
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const rooms = {
|
|
142
|
+
living: (c: Canvas, rand: Rand) => {
|
|
143
|
+
room(c, rand)
|
|
144
|
+
window(c, 380, 70, 200, 180)
|
|
145
|
+
const sofa: Rgb = pick(rand, [[90, 110, 140], [150, 90, 70], [110, 120, 100], [70, 70, 80]])
|
|
146
|
+
c.rect(60, 250, 260, 80, sofa)
|
|
147
|
+
c.rect(60, 220, 260, 40, sofa)
|
|
148
|
+
c.rect(120, 380, 160, 40, pick(rand, [[200, 170, 120], [180, 80, 70], [90, 120, 150]]))
|
|
149
|
+
c.circle(500, 400, 34, [120, 92, 60])
|
|
150
|
+
},
|
|
151
|
+
kitchen: (c: Canvas, rand: Rand) => {
|
|
152
|
+
room(c, rand)
|
|
153
|
+
const units: Rgb = pick(rand, [[250, 250, 250], [60, 72, 80], [120, 140, 120], [210, 196, 170]])
|
|
154
|
+
c.rect(40, 230, 560, 100, units)
|
|
155
|
+
c.rect(40, 220, 560, 12, [60, 60, 64])
|
|
156
|
+
c.rect(40, 60, 560, 90, units)
|
|
157
|
+
for (let x = 40; x < 600; x += 112) c.rect(x, 230, 2, 100, [180, 180, 180])
|
|
158
|
+
c.rect(260, 236, 120, 60, [40, 40, 44])
|
|
159
|
+
},
|
|
160
|
+
bedroom: (c: Canvas, rand: Rand) => {
|
|
161
|
+
room(c, rand)
|
|
162
|
+
window(c, 60, 80, 140, 150)
|
|
163
|
+
c.rect(250, 260, 300, 120, [240, 240, 236])
|
|
164
|
+
c.rect(250, 200, 300, 70, pick(rand, [[120, 90, 70], [80, 90, 110], [200, 190, 170]]))
|
|
165
|
+
c.rect(270, 240, 110, 30, [250, 250, 250])
|
|
166
|
+
c.rect(420, 240, 110, 30, [250, 250, 250])
|
|
167
|
+
c.rect(250, 300, 300, 80, pick(rand, [[150, 170, 200], [210, 160, 150], [180, 200, 170]]))
|
|
168
|
+
},
|
|
169
|
+
bathroom: (c: Canvas, rand: Rand) => {
|
|
170
|
+
c.rect(0, 0, c.width, c.height, pick(rand, [[236, 240, 242], [224, 230, 228], [242, 238, 232]]))
|
|
171
|
+
for (let x = 0; x < c.width; x += 40) c.rect(x, 0, 1, 330, [200, 204, 208])
|
|
172
|
+
for (let y = 0; y < 330; y += 40) c.rect(0, y, c.width, 1, [200, 204, 208])
|
|
173
|
+
c.rect(0, 330, c.width, 150, [120, 124, 130])
|
|
174
|
+
c.rect(60, 270, 300, 90, [252, 252, 252])
|
|
175
|
+
c.rect(440, 220, 120, 20, [252, 252, 252])
|
|
176
|
+
c.rect(470, 100, 60, 90, [190, 214, 230])
|
|
177
|
+
},
|
|
178
|
+
garden: (c: Canvas, rand: Rand) => {
|
|
179
|
+
sky(c, rand)
|
|
180
|
+
c.rect(0, 300, c.width, 180, [104, 160, 84])
|
|
181
|
+
c.rect(0, 250, c.width, 56, [52, 96, 56])
|
|
182
|
+
tree(c, 120, rand)
|
|
183
|
+
tree(c, 500, rand)
|
|
184
|
+
c.rect(250, 380, 140, 10, [160, 120, 80])
|
|
185
|
+
c.rect(260, 340, 10, 50, [160, 120, 80])
|
|
186
|
+
c.rect(370, 340, 10, 50, [160, 120, 80])
|
|
187
|
+
},
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export type RoomKind = keyof typeof rooms
|
|
191
|
+
|
|
192
|
+
/** A room inside a home, one of a few variants per kind. */
|
|
193
|
+
export const drawRoom = (kind: RoomKind, variant: number) => {
|
|
194
|
+
const c = createCanvas(photoWidth, photoHeight)
|
|
195
|
+
rooms[kind](c, rngAt(9200 + Object.keys(rooms).indexOf(kind), variant))
|
|
196
|
+
return c
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export const exteriorVariants = 6
|
|
200
|
+
export const roomVariants = 4
|
|
201
|
+
export const roomKinds = Object.keys(rooms) as RoomKind[]
|
|
202
|
+
export const propertyTypes = Object.keys(exteriors) as PropertyType[]
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import sharp from 'sharp'
|
|
3
|
+
import { formatUri, renderVariant, storeFile, thumbHash, variantPath } from '@protobase/server'
|
|
4
|
+
import { photoVariants } from '../../config/photo-variants'
|
|
5
|
+
import { drawExterior, drawRoom, exteriorVariants, propertyTypes, roomKinds, roomVariants, type PropertyType, type RoomKind } from './draw'
|
|
6
|
+
import { encodePng } from './png'
|
|
7
|
+
|
|
8
|
+
/** Where the listing photos sit in the public file provider. */
|
|
9
|
+
const folder = 'listings'
|
|
10
|
+
|
|
11
|
+
/** A home's outside, or one of its rooms, by its plain `public:` URI; the library gives the value a row stores. */
|
|
12
|
+
export const exteriorPhoto = (type: PropertyType, variant: number) => `public:${folder}/${type}-${(variant % exteriorVariants) + 1}.png`
|
|
13
|
+
export const roomPhoto = (kind: RoomKind, variant: number) => `public:${folder}/${kind}-${(variant % roomVariants) + 1}.png`
|
|
14
|
+
|
|
15
|
+
/** A library photo as a row stores it: its file value, naming its variants, and its placeholder. */
|
|
16
|
+
export type StoredPhoto = { value: string; placeholder: string }
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Draws every photo the listings use into the public file provider, as the server resolves it from `env`: its folder,
|
|
20
|
+
* `data/files/public` by default, or its bucket. The same files every run, a few dozen in all, each with the variants and
|
|
21
|
+
* placeholder an upload to the photo fields would get. Answers them by their plain URI.
|
|
22
|
+
*/
|
|
23
|
+
export const writePhotoLibrary = async (env: Record<string, string | undefined>) => {
|
|
24
|
+
const photos = new Map<string, StoredPhoto>()
|
|
25
|
+
const write = async (uri: string, canvas: { width: number; height: number; pixels: Uint8Array }) => {
|
|
26
|
+
const file = uri.slice('public:'.length)
|
|
27
|
+
const png = encodePng(canvas.width, canvas.height, canvas.pixels)
|
|
28
|
+
await storeFile({ provider: 'public', path: file, body: png, env })
|
|
29
|
+
const variants: Record<string, string> = {}
|
|
30
|
+
for (const [name, variant] of Object.entries(photoVariants)) {
|
|
31
|
+
const { data, extension } = await renderVariant(sharp, png, variant)
|
|
32
|
+
await storeFile({ provider: 'public', path: variantPath(file, name, extension), body: data, env })
|
|
33
|
+
variants[name] = extension
|
|
34
|
+
}
|
|
35
|
+
photos.set(uri, { value: formatUri({ provider: 'public', path: file, name: path.basename(file), size: png.length, variants }), placeholder: await thumbHash(sharp, png) })
|
|
36
|
+
}
|
|
37
|
+
for (const type of propertyTypes) {
|
|
38
|
+
for (let variant = 0; variant < exteriorVariants; variant++) await write(exteriorPhoto(type, variant), drawExterior(type, variant))
|
|
39
|
+
}
|
|
40
|
+
for (const kind of roomKinds) {
|
|
41
|
+
for (let variant = 0; variant < roomVariants; variant++) await write(roomPhoto(kind, variant), drawRoom(kind, variant))
|
|
42
|
+
}
|
|
43
|
+
return photos
|
|
44
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { deflateSync } from 'node:zlib'
|
|
2
|
+
|
|
3
|
+
const crcTable = Array.from({ length: 256 }, (_, n) => {
|
|
4
|
+
let c = n
|
|
5
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
|
|
6
|
+
return c >>> 0
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
const crc32 = (bytes: Uint8Array) => {
|
|
10
|
+
let c = 0xffffffff
|
|
11
|
+
for (const byte of bytes) c = crcTable[(c ^ byte) & 0xff]! ^ (c >>> 8)
|
|
12
|
+
return (c ^ 0xffffffff) >>> 0
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const chunk = (type: string, data: Uint8Array) => {
|
|
16
|
+
const body = Buffer.concat([Buffer.from(type, 'ascii'), data])
|
|
17
|
+
const length = Buffer.alloc(4)
|
|
18
|
+
length.writeUInt32BE(data.length)
|
|
19
|
+
const crc = Buffer.alloc(4)
|
|
20
|
+
crc.writeUInt32BE(crc32(body))
|
|
21
|
+
return Buffer.concat([length, body, crc])
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** An 8-bit RGB image as a PNG file: `pixels` holds width * height * 3 bytes, row by row. */
|
|
25
|
+
export const encodePng = (width: number, height: number, pixels: Uint8Array) => {
|
|
26
|
+
const header = Buffer.alloc(13)
|
|
27
|
+
header.writeUInt32BE(width, 0)
|
|
28
|
+
header.writeUInt32BE(height, 4)
|
|
29
|
+
header.set([8, 2, 0, 0, 0], 8)
|
|
30
|
+
// Each row starts with its filter type, 0 (none).
|
|
31
|
+
const raw = Buffer.alloc((width * 3 + 1) * height)
|
|
32
|
+
for (let y = 0; y < height; y++) raw.set(pixels.subarray(y * width * 3, (y + 1) * width * 3), y * (width * 3 + 1) + 1)
|
|
33
|
+
return Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk('IHDR', header), chunk('IDAT', deflateSync(raw)), chunk('IEND', new Uint8Array())])
|
|
34
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export const hash = (seed: number, index: number) => {
|
|
2
|
+
let h = (seed ^ Math.imul(index + 0x9e3779b9, 0x85ebca6b)) >>> 0
|
|
3
|
+
h = Math.imul(h ^ (h >>> 16), 0x85ebca6b)
|
|
4
|
+
h = Math.imul(h ^ (h >>> 13), 0xc2b2ae35)
|
|
5
|
+
return (h ^ (h >>> 16)) >>> 0
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// Independent stream per (seed, index): lets any row be rebuilt without generating its neighbours.
|
|
9
|
+
export const rngAt = (seed: number, index: number) => {
|
|
10
|
+
let state = hash(seed, index)
|
|
11
|
+
return () => {
|
|
12
|
+
state = (state + 0x6d2b79f5) >>> 0
|
|
13
|
+
let t = state
|
|
14
|
+
t = Math.imul(t ^ (t >>> 15), t | 1)
|
|
15
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61)
|
|
16
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type Rand = ReturnType<typeof rngAt>
|
|
21
|
+
|
|
22
|
+
export const pick = <T>(rand: Rand, items: readonly T[]) => items[Math.floor(rand() * items.length)]!
|
|
23
|
+
|
|
24
|
+
export const int = (rand: Rand, min: number, max: number) => min + Math.floor(rand() * (max - min + 1))
|
|
25
|
+
|
|
26
|
+
export const weighted = <T>(rand: Rand, entries: ReadonlyArray<readonly [T, number]>) => {
|
|
27
|
+
const total = entries.reduce((sum, [, weight]) => sum + weight, 0)
|
|
28
|
+
let roll = rand() * total
|
|
29
|
+
for (const [value, weight] of entries) {
|
|
30
|
+
roll -= weight
|
|
31
|
+
if (roll < 0) return value
|
|
32
|
+
}
|
|
33
|
+
return entries[entries.length - 1]![0]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const makeRng = (seed: number) => rngAt(seed, 0)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const scales = {
|
|
2
|
+
small: { listings: 400, agencies: 30, members: 24 },
|
|
3
|
+
medium: { listings: 20_000, agencies: 600, members: 2_000 },
|
|
4
|
+
large: { listings: 100_000, agencies: 2_500, members: 10_000 },
|
|
5
|
+
} as const
|
|
6
|
+
|
|
7
|
+
export type ScaleName = keyof typeof scales
|
|
8
|
+
|
|
9
|
+
export const isScale = (name: string | undefined): name is ScaleName => name !== undefined && name in scales
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Db } from '../../db/connection'
|
|
2
|
+
import type { StoredPhoto } from '../photos/library'
|
|
3
|
+
import { copyRows } from '../copy'
|
|
4
|
+
import { iso, row } from '../format'
|
|
5
|
+
import type { World } from '../world'
|
|
6
|
+
|
|
7
|
+
function* placeRows(world: World) {
|
|
8
|
+
for (const [index, place] of world.places.entries()) yield row(index + 1, place.name, place.region, place.country, place.latitude.toFixed(6), place.longitude.toFixed(6))
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function* agencyRows(world: World) {
|
|
12
|
+
for (const agency of world.agencies) yield row(agency.id, agency.name, agency.placeId, agency.phone, agency.email, agency.website, iso(Date.UTC(2024, 0, 1)), iso(Date.UTC(2024, 0, 1)))
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type Photos = ReadonlyMap<string, StoredPhoto>
|
|
16
|
+
|
|
17
|
+
// The library's stored value and placeholder of a photo the world names by its plain URI.
|
|
18
|
+
const storedPhoto = (photos: Photos, uri: string) => {
|
|
19
|
+
const stored = photos.get(uri)
|
|
20
|
+
if (!stored) throw new Error(`The photo library has no ${uri}`)
|
|
21
|
+
return stored
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function* listingRows(world: World, photos: Photos) {
|
|
25
|
+
for (const l of world.listings) {
|
|
26
|
+
const photo = storedPhoto(photos, l.photo)
|
|
27
|
+
yield row(
|
|
28
|
+
l.id, l.agencyId, l.placeId, l.propertyType, l.status, l.address, l.postalCode, l.latitude.toFixed(6), l.longitude.toFixed(6), l.positionExact,
|
|
29
|
+
l.purchasePrice?.toFixed(2), l.rentPrice?.toFixed(2), l.livingArea, l.plotArea, l.rooms, l.bedrooms, l.bathrooms, l.builtYear, l.energyLabel, l.garden, l.balcony, l.parking,
|
|
30
|
+
l.description, photo.value, photo.placeholder, l.agentName, l.agentPhone, l.publishedAt === null ? null : iso(l.publishedAt), iso(l.createdAt), iso(l.publishedAt ?? l.createdAt),
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function* photoRows(world: World, photos: Photos) {
|
|
36
|
+
for (const listing of world.listings) {
|
|
37
|
+
for (const photo of listing.photos) {
|
|
38
|
+
const stored = storedPhoto(photos, photo.photo)
|
|
39
|
+
yield row(listing.id, photo.position, stored.value, stored.placeholder, photo.caption)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The market's rows, each photo as the library stored it. */
|
|
45
|
+
export const seedMarket = async (sql: Db, world: World, photos: Photos) => {
|
|
46
|
+
await copyRows(sql, 'market.places (id, name, region, country, latitude, longitude)', placeRows(world))
|
|
47
|
+
await copyRows(sql, 'market.agencies (id, name, place_id, phone, email, website, created_at, updated_at)', agencyRows(world))
|
|
48
|
+
await copyRows(
|
|
49
|
+
sql,
|
|
50
|
+
'market.listings (id, agency_id, place_id, property_type, status, address, postal_code, latitude, longitude, position_exact, purchase_price, rent_price, living_area, plot_area, rooms, bedrooms, bathrooms, built_year, energy_label, garden, balcony, parking, description, photo, photo_placeholder, agent_name, agent_phone, published_at, created_at, updated_at)',
|
|
51
|
+
listingRows(world, photos),
|
|
52
|
+
)
|
|
53
|
+
await copyRows(sql, 'market.listing_photos (listing_id, position, photo, photo_placeholder, caption)', photoRows(world, photos))
|
|
54
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { hashPassword } from 'better-auth/crypto'
|
|
2
|
+
import type { Db } from '../../db/connection'
|
|
3
|
+
import { demoPassword } from '../builders/member'
|
|
4
|
+
import { copyRows } from '../copy'
|
|
5
|
+
import { iso, row } from '../format'
|
|
6
|
+
import type { World } from '../world'
|
|
7
|
+
|
|
8
|
+
function* userRows(world: World) {
|
|
9
|
+
for (const user of world.users) yield row(user.id, user.name, user.email, true, iso(user.createdAt), iso(user.createdAt), user.role, false, false)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Only the demo accounts can sign in: they get a password, the generated members none.
|
|
13
|
+
const accountRows = async (world: World) => {
|
|
14
|
+
const password = await hashPassword(demoPassword)
|
|
15
|
+
return world.users.filter((user) => user.demo).map((user) => row(`${user.id}-credential`, user.id, 'credential', user.id, password, iso(user.createdAt), iso(user.createdAt)))
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function* searchRows(world: World) {
|
|
19
|
+
for (const s of world.searches) yield row(s.id, s.userId, s.name, s.filter, s.alertFrequency, iso(s.seenAt), iso(s.createdAt))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function* alertRows(world: World) {
|
|
23
|
+
for (const alert of world.alerts) yield row(alert.search.userId, alert.search.id, alert.listing.id, iso(alert.matchedAt), alert.emailedAt === null ? null : iso(alert.emailedAt), alert.search.alertFrequency !== 'none')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function* favoriteRows(world: World) {
|
|
27
|
+
for (const { user, favorites } of world.saved) for (const favorite of favorites) yield row(user.id, favorite.listing.id, iso(favorite.createdAt))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function* noteRows(world: World) {
|
|
31
|
+
for (const { user, notes } of world.saved) for (const note of notes) yield row(user.id, note.listing.id, note.body, iso(note.createdAt), iso(note.createdAt))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const seedMembers = async (sql: Db, world: World) => {
|
|
35
|
+
await copyRows(sql, 'auth."user" (id, name, email, email_verified, created_at, updated_at, role, banned, two_factor_enabled)', userRows(world))
|
|
36
|
+
await copyRows(sql, 'auth.account (id, account_id, provider_id, user_id, password, created_at, updated_at)', await accountRows(world))
|
|
37
|
+
await copyRows(sql, 'members.saved_searches (id, user_id, name, filter, alert_frequency, seen_at, created_at)', searchRows(world))
|
|
38
|
+
await copyRows(sql, 'members.alerts (user_id, saved_search_id, listing_id, matched_at, emailed_at, mail_due)', alertRows(world))
|
|
39
|
+
await copyRows(sql, 'members.favorites (user_id, listing_id, created_at)', favoriteRows(world))
|
|
40
|
+
await copyRows(sql, 'members.notes (user_id, listing_id, body, created_at, updated_at)', noteRows(world))
|
|
41
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { readSearchFilter } from '../config/saved_searches/filter'
|
|
2
|
+
import { fitsSearch, type ListingRecord } from '../jobs/match'
|
|
3
|
+
import { agencyAt, type SeededAgency } from './builders/agency'
|
|
4
|
+
import { distanceKm } from './builders/distance'
|
|
5
|
+
import { listingAt, type SeededListing } from './builders/listing'
|
|
6
|
+
import { demoSearches, demoUsers, memberAt, savedBy, searchesOf, type SeededSearch, type SeededUser } from './builders/member'
|
|
7
|
+
import { DAY, seedNow } from './calendar'
|
|
8
|
+
import { places } from './data/places'
|
|
9
|
+
import { int, rngAt } from './rng'
|
|
10
|
+
import { scales, type ScaleName } from './scales'
|
|
11
|
+
|
|
12
|
+
export type SeededAlert = { search: SeededSearch; listing: SeededListing; matchedAt: number; emailedAt: number | null }
|
|
13
|
+
|
|
14
|
+
/** Alerts go back this far: what members heard of recently. */
|
|
15
|
+
const alertWindow = 30 * DAY
|
|
16
|
+
|
|
17
|
+
// The daily digest goes out at 7 in Amsterdam: 05:00 UTC in summer time, close enough for the sample.
|
|
18
|
+
const nextDigest = (after: number) => {
|
|
19
|
+
const day = Math.floor(after / DAY) * DAY + 5 * 3_600_000
|
|
20
|
+
return day > after ? day : day + DAY
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// The listing as the match-listing job reads its row when it is published: available, by field name.
|
|
24
|
+
const recordOf = (listing: SeededListing): ListingRecord => ({
|
|
25
|
+
id: listing.id, agency_id: listing.agencyId, place_id: listing.placeId, property_type: listing.propertyType, status: 'available',
|
|
26
|
+
address: listing.address, postal_code: listing.postalCode, latitude: listing.latitude, longitude: listing.longitude,
|
|
27
|
+
purchase_price: listing.purchasePrice, rent_price: listing.rentPrice, for_sale: listing.purchasePrice !== null, for_rent: listing.rentPrice !== null,
|
|
28
|
+
living_area: listing.livingArea, plot_area: listing.plotArea, rooms: listing.rooms, bedrooms: listing.bedrooms, bathrooms: listing.bathrooms,
|
|
29
|
+
built_year: listing.builtYear, energy_label: listing.energyLabel, garden: listing.garden, balcony: listing.balcony, parking: listing.parking,
|
|
30
|
+
published_at: new Date(listing.publishedAt!),
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
// What the filter would refuse anyway, tested first so that the rule runs on fewer listings: the way it is offered and
|
|
34
|
+
// the type.
|
|
35
|
+
const mayFit = ({ criteria }: SeededSearch, listing: SeededListing) =>
|
|
36
|
+
(criteria.forSale === criteria.forRent && !criteria.forSale) || (criteria.forSale && listing.purchasePrice !== null) || (criteria.forRent && listing.rentPrice !== null)
|
|
37
|
+
? criteria.propertyType === null || criteria.propertyType === listing.propertyType
|
|
38
|
+
: false
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The alerts the match-listing job would have recorded: for every saved search, each listing published in the window,
|
|
42
|
+
* after the search was saved, that fit it when it was published, as available, by the same rule the job uses. Only the
|
|
43
|
+
* listings in a search's places, of its type and offered its way are tried; the rule decides.
|
|
44
|
+
*/
|
|
45
|
+
const alertsFor = (searches: SeededSearch[], listings: SeededListing[]) => {
|
|
46
|
+
const recent = listings.filter((listing) => listing.publishedAt !== null && listing.publishedAt > seedNow - alertWindow)
|
|
47
|
+
const byPlace = new Map<number, SeededListing[]>()
|
|
48
|
+
for (const listing of recent) byPlace.set(listing.placeId, [...(byPlace.get(listing.placeId) ?? []), listing])
|
|
49
|
+
const records = new Map(recent.map((listing) => [listing.id, recordOf(listing)]))
|
|
50
|
+
const alerts: SeededAlert[] = []
|
|
51
|
+
for (const search of searches) {
|
|
52
|
+
const filter = readSearchFilter(search.filter)
|
|
53
|
+
const rand = rngAt(601, search.id)
|
|
54
|
+
const candidates = search.criteria.placeIds.length === 0 ? recent : search.criteria.placeIds.flatMap((id) => byPlace.get(id) ?? [])
|
|
55
|
+
for (const listing of candidates) {
|
|
56
|
+
if (listing.publishedAt! < search.createdAt || !mayFit(search, listing) || !fitsSearch(filter, records.get(listing.id)!)) continue
|
|
57
|
+
const matchedAt = listing.publishedAt! + int(rand, 1, 5) * 60_000
|
|
58
|
+
const mailed = search.alertFrequency === 'instant' ? matchedAt + 60_000 : search.alertFrequency === 'daily' ? nextDigest(matchedAt) : null
|
|
59
|
+
alerts.push({ search, listing, matchedAt, emailedAt: mailed !== null && mailed <= seedNow ? mailed : null })
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return alerts
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Everything the seed writes, built from the scale alone: the same rows every run. */
|
|
66
|
+
export const buildWorld = (scale: ScaleName) => {
|
|
67
|
+
const counts = scales[scale]
|
|
68
|
+
const agencies = Array.from({ length: counts.agencies }, (_, ordinal) => agencyAt(places, ordinal))
|
|
69
|
+
const agenciesByPlace = new Map<number, SeededAgency[]>()
|
|
70
|
+
for (const agency of agencies) agenciesByPlace.set(agency.placeId, [...(agenciesByPlace.get(agency.placeId) ?? []), agency])
|
|
71
|
+
const agenciesByCountry = { NL: agencies.filter((agency) => agency.country === 'NL'), ES: agencies.filter((agency) => agency.country === 'ES') }
|
|
72
|
+
if (agenciesByCountry.NL.length === 0 || agenciesByCountry.ES.length === 0) throw new Error('Every country needs an agency: raise the scale\'s agencies')
|
|
73
|
+
const listings = Array.from({ length: counts.listings }, (_, ordinal) => listingAt(places, agenciesByPlace, agenciesByCountry, ordinal))
|
|
74
|
+
|
|
75
|
+
const generated = Array.from({ length: counts.members - 2 }, (_, ordinal) => memberAt(ordinal))
|
|
76
|
+
const users: SeededUser[] = [...demoUsers, ...generated]
|
|
77
|
+
const searches = demoSearches(places)
|
|
78
|
+
for (const [ordinal, user] of generated.entries()) searches.push(...searchesOf(places, user, ordinal, searches.length + 1))
|
|
79
|
+
|
|
80
|
+
// Favorites among the published listings offered the way a member's first search looks, in or near its places.
|
|
81
|
+
const published = listings.filter((listing) => listing.publishedAt !== null)
|
|
82
|
+
const publishedIn = new Map<number, SeededListing[]>()
|
|
83
|
+
for (const listing of published) publishedIn.set(listing.placeId, [...(publishedIn.get(listing.placeId) ?? []), listing])
|
|
84
|
+
const shown = (search: SeededSearch | undefined) => {
|
|
85
|
+
if (!search) return []
|
|
86
|
+
const near = new Set(search.criteria.placeIds.flatMap((id) => places.flatMap((place, index) => (distanceKm(places[id - 1]!, place) <= 10 ? [index + 1] : []))))
|
|
87
|
+
const pool = near.size > 0 ? [...near].flatMap((id) => publishedIn.get(id) ?? []) : published
|
|
88
|
+
return pool.filter((listing) => (search.criteria.forSale && listing.purchasePrice !== null) || (search.criteria.forRent && listing.rentPrice !== null))
|
|
89
|
+
}
|
|
90
|
+
const saved = users.filter((user) => user.role === 'member').map((user, ordinal) => {
|
|
91
|
+
const candidates = shown(searches.find((search) => search.userId === user.id))
|
|
92
|
+
return { user, ...savedBy(user, ordinal, candidates, user.demo ? 5 : int(rngAt(504, ordinal), 0, 6)) }
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
return { places, agencies, listings, users, searches, saved, alerts: alertsFor(searches, listings) }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export type World = ReturnType<typeof buildWorld>
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./.protobase/tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"target": "ES2023",
|
|
5
|
+
"lib": [
|
|
6
|
+
"ES2023",
|
|
7
|
+
"DOM",
|
|
8
|
+
"DOM.Iterable"
|
|
9
|
+
],
|
|
10
|
+
"module": "ESNext",
|
|
11
|
+
"moduleResolution": "Bundler",
|
|
12
|
+
"jsx": "react-jsx",
|
|
13
|
+
"strict": true,
|
|
14
|
+
"noUncheckedIndexedAccess": true,
|
|
15
|
+
"verbatimModuleSyntax": true,
|
|
16
|
+
"isolatedModules": true,
|
|
17
|
+
"skipLibCheck": true,
|
|
18
|
+
"noEmit": true
|
|
19
|
+
},
|
|
20
|
+
"exclude": [
|
|
21
|
+
"node_modules",
|
|
22
|
+
"dist"
|
|
23
|
+
]
|
|
24
|
+
}
|