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.
- 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,53 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import type { AlertedListing } from './alerted-listing'
|
|
3
|
+
import { prices } from './alerted-listing'
|
|
4
|
+
import { dailyDigest } from './daily-digest'
|
|
5
|
+
import { instantAlert } from './instant-alert'
|
|
6
|
+
|
|
7
|
+
const app = { name: 'Property portal', url: 'https://portal.example.com/' }
|
|
8
|
+
const canalFlat = { name: 'Canal flat', url: 'https://portal.example.com/saved_searches/3' }
|
|
9
|
+
const flat: AlertedListing = { address: 'Keizersgracht 123-2', place: 'Amsterdam', purchasePrice: '650000.00', rentPrice: null, livingArea: 92, url: 'https://portal.example.com/listings/42', searches: [canalFlat] }
|
|
10
|
+
const piso: AlertedListing = {
|
|
11
|
+
address: 'Calle de Serrano, 45', place: 'Madrid', purchasePrice: null, rentPrice: '2100.00', livingArea: 80, url: 'https://portal.example.com/listings/7',
|
|
12
|
+
searches: [{ name: 'Madrid centro', url: 'https://portal.example.com/saved_searches/5' }],
|
|
13
|
+
}
|
|
14
|
+
const searchesUrl = 'https://portal.example.com/saved_searches'
|
|
15
|
+
|
|
16
|
+
describe('the instant alert', () => {
|
|
17
|
+
it('names the one listing, with its price, size, search and link', async () => {
|
|
18
|
+
const email = await instantAlert.render({ app, listings: [flat], searchesUrl })
|
|
19
|
+
expect(email.subject).toBe('New: Keizersgracht 123-2, Amsterdam')
|
|
20
|
+
expect(email.html).toContain('href="https://portal.example.com/listings/42"')
|
|
21
|
+
expect(email.text).toContain('For sale: €650,000, 92 m²')
|
|
22
|
+
expect(email.text).toContain('Fits "Canal flat" https://portal.example.com/saved_searches/3')
|
|
23
|
+
expect(email.text).toContain(searchesUrl)
|
|
24
|
+
expect(email.text).toContain('Property portal https://portal.example.com/')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('names every search a listing fits once, one saved without a name as one of theirs', async () => {
|
|
28
|
+
const both = { ...flat, searches: [canalFlat, { name: null, url: 'https://portal.example.com/saved_searches/9' }] }
|
|
29
|
+
const email = await instantAlert.render({ app, listings: [both], searchesUrl })
|
|
30
|
+
expect(email.text).toContain('Fits "Canal flat" https://portal.example.com/saved_searches/3 and one of your searches https://portal.example.com/saved_searches/9')
|
|
31
|
+
expect(email.text.match(/Keizersgracht 123-2/g)).toHaveLength(1)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('counts the listings when several came at once', async () => {
|
|
35
|
+
expect((await instantAlert.render({ app, listings: [flat, piso], searchesUrl })).subject).toBe('2 new homes fit your searches')
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
describe('the daily digest', () => {
|
|
40
|
+
it('counts the listings of the day, and gives a rent per month', async () => {
|
|
41
|
+
const email = await dailyDigest.render({ app, listings: [flat, piso], searchesUrl })
|
|
42
|
+
expect(email.subject).toBe('2 new homes fit your searches')
|
|
43
|
+
expect(email.text).toContain('These homes were published since your last digest')
|
|
44
|
+
expect(email.text).toContain('For rent: €2,100 a month, 80 m²')
|
|
45
|
+
expect((await dailyDigest.render({ app, listings: [piso], searchesUrl })).subject).toBe('1 new home fits your searches')
|
|
46
|
+
})
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
describe('prices', () => {
|
|
50
|
+
it('gives both prices of a home offered both ways', () => {
|
|
51
|
+
expect(prices({ purchasePrice: '650000.00', rentPrice: '2450.00' })).toBe('For sale: €650,000 or for rent: €2,450 a month')
|
|
52
|
+
})
|
|
53
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { defineEmail } from '@protobase/server'
|
|
2
|
+
import { EmailLayout, type MailingApp } from '@protobase/server/email'
|
|
3
|
+
import { ListingBlock, SearchesFooter, type AlertedListing } from './alerted-listing'
|
|
4
|
+
|
|
5
|
+
/** The new listings that fit a member's instant searches since their last mail: usually one. */
|
|
6
|
+
export type InstantAlert = { app: MailingApp; listings: AlertedListing[]; searchesUrl: string }
|
|
7
|
+
|
|
8
|
+
const InstantAlertEmail = ({ app, listings, searchesUrl }: InstantAlert) => (
|
|
9
|
+
<EmailLayout
|
|
10
|
+
app={app}
|
|
11
|
+
preview={`${listings.length === 1 ? `${listings[0]!.address}, ${listings[0]!.place}` : `${listings.length} new homes`} fit your search`}
|
|
12
|
+
heading={listings.length === 1 ? 'A new home fits your search' : 'New homes fit your searches'}
|
|
13
|
+
>
|
|
14
|
+
{listings.map((listing) => (
|
|
15
|
+
<ListingBlock key={listing.url} listing={listing} />
|
|
16
|
+
))}
|
|
17
|
+
<SearchesFooter searchesUrl={searchesUrl} />
|
|
18
|
+
</EmailLayout>
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
/** Mailed as soon as a listing fits a search set to mail at once. */
|
|
22
|
+
export const instantAlert = defineEmail({
|
|
23
|
+
name: 'instant-alert',
|
|
24
|
+
subject: ({ listings }: InstantAlert) => (listings.length === 1 ? `New: ${listings[0]!.address}, ${listings[0]!.place}` : `${listings.length} new homes fit your searches`),
|
|
25
|
+
component: InstantAlertEmail,
|
|
26
|
+
})
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { alertedListings, type AlertRow } from './alerted-listings'
|
|
3
|
+
|
|
4
|
+
const base = 'https://portal.example.com'
|
|
5
|
+
// Two alerts for one flat from two searches, and one for a house, as the email-alerts job reads them.
|
|
6
|
+
const flat: AlertRow = { listing_id: '42', address: 'Spaarne 12', place: 'Haarlem', purchase_price: '450000.00', rent_price: null, living_area: 85, search_id: '3', search: 'Haarlem' }
|
|
7
|
+
const house: AlertRow = { ...flat, listing_id: '7', address: 'Dorpsstraat 1' }
|
|
8
|
+
|
|
9
|
+
describe('alertedListings', () => {
|
|
10
|
+
it('mails each listing once, with every search of the member it fits, in the order found', () => {
|
|
11
|
+
const listings = alertedListings([flat, house, { ...flat, search_id: '5', search: null }], base)
|
|
12
|
+
expect(listings.map((listing) => listing.url)).toEqual([`${base}/listings/42`, `${base}/listings/7`])
|
|
13
|
+
expect(listings[0]!.searches).toEqual([
|
|
14
|
+
{ name: 'Haarlem', url: `${base}/saved_searches/3` },
|
|
15
|
+
{ name: null, url: `${base}/saved_searches/5` },
|
|
16
|
+
])
|
|
17
|
+
expect(listings[0]).toMatchObject({ purchasePrice: '450000.00', rentPrice: null, livingArea: 85 })
|
|
18
|
+
})
|
|
19
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { appPath } from '@protobase/schema'
|
|
2
|
+
import type { AlertedListing } from '../emails/alerted-listing'
|
|
3
|
+
|
|
4
|
+
/** An alert due for mail as the email-alerts job reads it: the listing it found and the search it fits, decimals as text. */
|
|
5
|
+
export type AlertRow = {
|
|
6
|
+
listing_id: string | number
|
|
7
|
+
address: string
|
|
8
|
+
place: string
|
|
9
|
+
purchase_price: string | number | null
|
|
10
|
+
rent_price: string | number | null
|
|
11
|
+
living_area: number
|
|
12
|
+
search_id: string | number
|
|
13
|
+
search: string | null
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const amount = (value: string | number | null) => (value === null ? null : String(value))
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The listings of a member's alerts, in the order they were found, each once with every search of theirs it fits, and
|
|
20
|
+
* links on the app at `base`.
|
|
21
|
+
*/
|
|
22
|
+
export const alertedListings = (rows: AlertRow[], base: string): AlertedListing[] => {
|
|
23
|
+
const listings = new Map<string, AlertedListing>()
|
|
24
|
+
for (const row of rows) {
|
|
25
|
+
const search = { name: row.search, url: `${base}${appPath({ kind: 'record', resource: 'saved_searches', key: String(row.search_id) })}` }
|
|
26
|
+
const known = listings.get(String(row.listing_id))
|
|
27
|
+
if (known) {
|
|
28
|
+
known.searches.push(search)
|
|
29
|
+
continue
|
|
30
|
+
}
|
|
31
|
+
listings.set(String(row.listing_id), {
|
|
32
|
+
address: row.address,
|
|
33
|
+
place: row.place,
|
|
34
|
+
purchasePrice: amount(row.purchase_price),
|
|
35
|
+
rentPrice: amount(row.rent_price),
|
|
36
|
+
livingArea: row.living_area,
|
|
37
|
+
url: `${base}${appPath({ kind: 'record', resource: 'listings', key: String(row.listing_id) })}`,
|
|
38
|
+
searches: [search],
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
return [...listings.values()]
|
|
42
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { enqueueJob, type JobRun } from '@protobase/server'
|
|
2
|
+
import { offeredStatuses } from './offered'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Declared in protobase.config.ts, every morning at 7 in Amsterdam: a digest for each member whose daily searches found listings due for mail, not mailed
|
|
6
|
+
* yet and still on offer.
|
|
7
|
+
*/
|
|
8
|
+
export default (async (_, { db }) => {
|
|
9
|
+
const waiting = await db
|
|
10
|
+
.selectFrom('members.alerts as a')
|
|
11
|
+
.innerJoin('members.saved_searches as s', 's.id', 'a.saved_search_id')
|
|
12
|
+
.innerJoin('market.listings as l', 'l.id', 'a.listing_id')
|
|
13
|
+
.select('a.user_id')
|
|
14
|
+
.distinct()
|
|
15
|
+
.where('a.emailed_at', 'is', null)
|
|
16
|
+
.where('a.mail_due', '=', true)
|
|
17
|
+
.where('l.status', 'in', offeredStatuses)
|
|
18
|
+
.where('s.alert_frequency', '=', 'daily')
|
|
19
|
+
.execute()
|
|
20
|
+
for (const { user_id } of waiting) await enqueueJob(db, 'email-alerts', { userId: user_id, frequency: 'daily' }, { key: `daily:${user_id}` })
|
|
21
|
+
}) satisfies JobRun
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { JobRun } from '@protobase/server'
|
|
2
|
+
import { appPath } from '@protobase/schema'
|
|
3
|
+
import { dailyDigest } from '../emails/daily-digest'
|
|
4
|
+
import { instantAlert } from '../emails/instant-alert'
|
|
5
|
+
import { alertedListings } from './alerted-listings'
|
|
6
|
+
import { offeredStatuses } from './offered'
|
|
7
|
+
|
|
8
|
+
/** The app's public address, for the links in the mail. */
|
|
9
|
+
const baseUrl = () => process.env.PROTOBASE_URL ?? 'http://localhost:5173'
|
|
10
|
+
|
|
11
|
+
/** The app as its mails name it: its name (PROTOBASE_APP_NAME), or its host without one. */
|
|
12
|
+
const mailingApp = (base: string) => ({ name: process.env.PROTOBASE_APP_NAME || new URL(base).host, url: `${base}/` })
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Declared in protobase.config.ts. Mails a member the alerts due for mail (db/migrations/013_alert_mail.sql) of their searches that now mail with this
|
|
16
|
+
* frequency and have not gone out yet, of listings still on offer (jobs/offered.ts), in one message: the instant alert or
|
|
17
|
+
* the daily digest, each listing once with every search it fits. Then it marks them mailed. Without mail (no
|
|
18
|
+
* PROTOBASE_SMTP_URL) it leaves them for the app to show. A try that fails after sending sends them again on the retry:
|
|
19
|
+
* the mark comes after the send so no alert is marked that never went out.
|
|
20
|
+
*/
|
|
21
|
+
export default (async ({ userId, frequency }, { db, mail }) => {
|
|
22
|
+
if (!mail) return
|
|
23
|
+
const rows = await db
|
|
24
|
+
.selectFrom('members.alerts as a')
|
|
25
|
+
.innerJoin('members.saved_searches as s', 's.id', 'a.saved_search_id')
|
|
26
|
+
.innerJoin('market.listings as l', 'l.id', 'a.listing_id')
|
|
27
|
+
.innerJoin('market.places as pl', 'pl.id', 'l.place_id')
|
|
28
|
+
.select(['a.id', 'l.id as listing_id', 'l.address', 'pl.name as place', 'l.purchase_price', 'l.rent_price', 'l.living_area', 's.id as search_id', 's.name as search'])
|
|
29
|
+
.where('a.user_id', '=', userId)
|
|
30
|
+
.where('a.emailed_at', 'is', null)
|
|
31
|
+
.where('a.mail_due', '=', true)
|
|
32
|
+
.where('l.status', 'in', offeredStatuses)
|
|
33
|
+
.where('s.alert_frequency', '=', frequency)
|
|
34
|
+
.orderBy('a.matched_at')
|
|
35
|
+
.orderBy('a.id')
|
|
36
|
+
.execute()
|
|
37
|
+
if (rows.length === 0) return
|
|
38
|
+
const user = await db.selectFrom('auth.user').select('email').where('id', '=', userId).executeTakeFirst()
|
|
39
|
+
if (!user) return
|
|
40
|
+
const base = baseUrl()
|
|
41
|
+
const data = { app: mailingApp(base), listings: alertedListings(rows, base), searchesUrl: `${base}${appPath({ kind: 'list', resource: 'saved_searches' })}` }
|
|
42
|
+
await (frequency === 'daily' ? dailyDigest.send(user.email, data, mail) : instantAlert.send(user.email, data, mail))
|
|
43
|
+
await db.updateTable('members.alerts').set({ emailed_at: new Date() }).where('id', 'in', rows.map((row) => row.id)).execute()
|
|
44
|
+
}) satisfies JobRun<{ userId: string; frequency: 'instant' | 'daily' }>
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { enqueueJob, type JobRun } from '@protobase/server'
|
|
2
|
+
import { readSearchFilter } from '../config/saved_searches/filter'
|
|
3
|
+
import { fitsSearch } from './match'
|
|
4
|
+
import { isOffered } from './offered'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Declared in protobase.config.ts. Enqueued by Postgres when a listing is published (db/migrations/005_listing_published.sql): records an alert for each
|
|
8
|
+
* saved search the listing fits while it is on offer (jobs/offered.ts), due for mail when the search sends email
|
|
9
|
+
* (db/migrations/013_alert_mail.sql), and has those of searches that mail at once mailed. Running it twice records
|
|
10
|
+
* nothing twice.
|
|
11
|
+
*/
|
|
12
|
+
export default (async ({ listingId }, { db }) => {
|
|
13
|
+
const listing = await db.selectFrom('market.listings').selectAll().where('id', '=', listingId).where('published_at', 'is not', null).executeTakeFirst()
|
|
14
|
+
// Gone or back to a draft since it was published, or no longer on offer: nothing to tell anyone.
|
|
15
|
+
if (!listing || !isOffered(listing.status)) return
|
|
16
|
+
|
|
17
|
+
const searches = await db.selectFrom('members.saved_searches').select(['id', 'user_id', 'filter', 'alert_frequency']).execute()
|
|
18
|
+
const fitting = searches.filter((search) => fitsSearch(readSearchFilter(search.filter), listing))
|
|
19
|
+
if (fitting.length === 0) return
|
|
20
|
+
|
|
21
|
+
const recorded = await db
|
|
22
|
+
.insertInto('members.alerts')
|
|
23
|
+
.values(fitting.map((search) => ({ user_id: search.user_id, saved_search_id: search.id, listing_id: listingId, mail_due: search.alert_frequency !== 'none' })))
|
|
24
|
+
.onConflict((conflict) => conflict.columns(['saved_search_id', 'listing_id']).doNothing())
|
|
25
|
+
.returning(['user_id', 'saved_search_id'])
|
|
26
|
+
.execute()
|
|
27
|
+
const instant = new Set(fitting.filter((search) => search.alert_frequency === 'instant').map((search) => String(search.id)))
|
|
28
|
+
const users = new Set(recorded.filter((alert) => instant.has(String(alert.saved_search_id))).map((alert) => alert.user_id as string))
|
|
29
|
+
for (const userId of users) await enqueueJob(db, 'email-alerts', { userId, frequency: 'instant' }, { key: `instant:${userId}` })
|
|
30
|
+
}) satisfies JobRun<{ listingId: number }>
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { readSearchFilter } from '../config/saved_searches/filter'
|
|
3
|
+
import { fitsSearch } from './match'
|
|
4
|
+
|
|
5
|
+
// A three-room apartment for sale in Haarlem (place 2), as the database row reads: decimals as text, bigints too.
|
|
6
|
+
const listing = {
|
|
7
|
+
id: '42', place_id: 2, agency_id: 7, property_type: 'apartment', status: 'available', address: 'Spaarne 12', postal_code: '2011 CH',
|
|
8
|
+
latitude: '52.381200', longitude: '4.637100', purchase_price: '450000.00', rent_price: null, for_sale: true, for_rent: false,
|
|
9
|
+
living_area: 85, price_per_m2: '5294.12', rooms: 3, bedrooms: 2, energy_label: 'b', garden: false, balcony: true, parking: false,
|
|
10
|
+
published_at: new Date('2026-10-12T09:00:00Z'),
|
|
11
|
+
}
|
|
12
|
+
// The same flat, let in the meantime.
|
|
13
|
+
const both = { ...listing, rent_price: '1450.00', for_rent: true }
|
|
14
|
+
|
|
15
|
+
const fits = (filter: string, record: Record<string, unknown> = listing) => fitsSearch(readSearchFilter(filter), record)
|
|
16
|
+
|
|
17
|
+
describe('fitsSearch', () => {
|
|
18
|
+
it('fits every listing to a search without a filter', () => {
|
|
19
|
+
expect(fits('')).toBe(true)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('takes the filters of the listings page, as its address holds them', () => {
|
|
23
|
+
expect(fits('for_sale = true AND in(place_id, "1", "2") AND property_type = "apartment"')).toBe(true)
|
|
24
|
+
expect(fits('for_sale = true AND in(place_id, "1", "3")')).toBe(false)
|
|
25
|
+
expect(fits('place_id = "2" AND agency_id = 7')).toBe(true)
|
|
26
|
+
expect(fits('purchase_price >= 300000 AND purchase_price <= 450000 AND bedrooms >= 2 AND bedrooms <= 3')).toBe(true)
|
|
27
|
+
expect(fits('purchase_price >= 300000 AND purchase_price <= 449999')).toBe(false)
|
|
28
|
+
expect(fits('in(energy_label, "a", "b") AND balcony = true')).toBe(true)
|
|
29
|
+
expect(fits('garden = true')).toBe(false)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('fits a listing offered both ways to a search for either', () => {
|
|
33
|
+
expect(fits('for_rent = true', both)).toBe(true)
|
|
34
|
+
expect(fits('for_rent = true')).toBe(false)
|
|
35
|
+
expect(fits('(for_sale = true OR for_rent = true) AND rent_price <= 1500', both)).toBe(true)
|
|
36
|
+
// Without a rent the rent range keeps it out, as the list does
|
|
37
|
+
expect(fits('rent_price >= 0 AND rent_price <= 1500')).toBe(false)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('looks for the search text in the address and postal code', () => {
|
|
41
|
+
expect(fits('search("spaarne")')).toBe(true)
|
|
42
|
+
expect(fits('spaarne AND for_sale = true')).toBe(true)
|
|
43
|
+
expect(fits('search("2011")')).toBe(true)
|
|
44
|
+
expect(fits('search("gracht")')).toBe(false)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('fits nothing to a filter that cannot be read', () => {
|
|
48
|
+
expect(fits('purchase_price <=')).toBe(false)
|
|
49
|
+
expect(fits('swimming_pool = true')).toBe(false)
|
|
50
|
+
})
|
|
51
|
+
})
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { evaluateFilter } from '@protobase/schema'
|
|
2
|
+
import { listingsModel, type SearchFilter } from '../config/saved_searches/filter'
|
|
3
|
+
|
|
4
|
+
/** A listing as matching reads it: its fields by name, as the database row holds them. */
|
|
5
|
+
export type ListingRecord = Record<string, unknown>
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Whether a listing fits a saved search: the search's filter holds for it, read as the listings page's list reads it,
|
|
9
|
+
* its search text in the listings' search fields too. A filter that cannot be read fits nothing; an empty one fits
|
|
10
|
+
* every listing.
|
|
11
|
+
*/
|
|
12
|
+
export const fitsSearch = (search: SearchFilter, listing: ListingRecord, now = new Date()) => {
|
|
13
|
+
if (!search.ok) return false
|
|
14
|
+
return search.filter === undefined || evaluateFilter(search.filter, listing, { search: listingsModel, now: () => now }) === true
|
|
15
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { listingStatuses } from '../config/listings/data'
|
|
3
|
+
import { isOffered } from './offered'
|
|
4
|
+
|
|
5
|
+
describe('isOffered', () => {
|
|
6
|
+
it('tells of a home available or under offer, not one sold or let', () => {
|
|
7
|
+
expect(listingStatuses.filter(isOffered)).toEqual(['available', 'under_offer'])
|
|
8
|
+
})
|
|
9
|
+
})
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { listingStatuses } from '../config/listings/data'
|
|
2
|
+
|
|
3
|
+
type ListingStatus = (typeof listingStatuses)[number]
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The statuses of a home still on offer, which alerts tell of: available, or under offer, which may still fall through.
|
|
7
|
+
* A home sold or let is not new to anyone looking.
|
|
8
|
+
*/
|
|
9
|
+
export const offeredStatuses = ['available', 'under_offer'] as const satisfies readonly ListingStatus[]
|
|
10
|
+
|
|
11
|
+
/** Whether a listing with this status is still on offer. */
|
|
12
|
+
export const isOffered = (status: string) => (offeredStatuses as readonly string[]).includes(status)
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { proxiedStyle, tilesBucket, tilesProxy } from './tiles-proxy'
|
|
3
|
+
|
|
4
|
+
const style = JSON.stringify({
|
|
5
|
+
sources: { openmaptiles: { type: 'vector', url: 'pmtiles://https://map.rentnudge.com/20250819.pmtiles' } },
|
|
6
|
+
sprite: 'https://map.rentnudge.com/sprites/v4/light',
|
|
7
|
+
glyphs: 'https://map.rentnudge.com/fonts/{fontstack}/{range}.pbf',
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
// The bucket as the proxy sees it: what it was asked, and the answer it gives.
|
|
11
|
+
const bucket = (answer: () => Response) => {
|
|
12
|
+
const asked: Array<{ url: string; init: RequestInit | undefined }> = []
|
|
13
|
+
const upstream = (async (url: string, init?: RequestInit) => {
|
|
14
|
+
asked.push({ url, init })
|
|
15
|
+
return answer()
|
|
16
|
+
}) as typeof fetch
|
|
17
|
+
return { asked, proxy: tilesProxy(upstream) }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe('proxiedStyle', () => {
|
|
21
|
+
it("points the style's archive, sprite and fonts at the proxy, and credits OpenMapTiles and OpenStreetMap", () => {
|
|
22
|
+
expect(JSON.parse(proxiedStyle(style, 'http://localhost:8790'))).toEqual({
|
|
23
|
+
sources: { openmaptiles: { type: 'vector', url: 'pmtiles://http://localhost:8790/20250819.pmtiles', attribution: expect.stringContaining('© OpenStreetMap contributors') } },
|
|
24
|
+
sprite: 'http://localhost:8790/sprites/v4/light',
|
|
25
|
+
glyphs: 'http://localhost:8790/fonts/{fontstack}/{range}.pbf',
|
|
26
|
+
})
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('keeps the credit a source has', () => {
|
|
30
|
+
const credited = JSON.stringify({ sources: { own: { type: 'vector', url: 'pmtiles://https://map.rentnudge.com/x.pmtiles', attribution: '© Us' } } })
|
|
31
|
+
expect(JSON.parse(proxiedStyle(credited, 'http://localhost:8790')).sources.own.attribution).toBe('© Us')
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
describe('tilesProxy', () => {
|
|
36
|
+
it('reads a part of the archive from the bucket, with the headers pmtiles needs and CORS for any origin', async () => {
|
|
37
|
+
const { asked, proxy } = bucket(() => new Response('tile', { status: 206, headers: { 'content-type': 'application/octet-stream', 'content-range': 'bytes 0-3/2140453952', etag: '"v1"', server: 'cloudflare' } }))
|
|
38
|
+
const answer = await proxy(new Request('http://localhost:8790/20250819.pmtiles', { headers: { range: 'bytes=0-3', origin: 'http://localhost:5173' } }))
|
|
39
|
+
expect(asked).toEqual([{ url: `${tilesBucket}/20250819.pmtiles`, init: { method: 'GET', headers: { range: 'bytes=0-3' } } }])
|
|
40
|
+
expect(answer.status).toBe(206)
|
|
41
|
+
expect(await answer.text()).toBe('tile')
|
|
42
|
+
expect(Object.fromEntries(answer.headers)).toEqual({
|
|
43
|
+
'access-control-allow-origin': '*',
|
|
44
|
+
'access-control-allow-headers': 'range, if-match',
|
|
45
|
+
'access-control-expose-headers': 'content-range, etag',
|
|
46
|
+
'content-type': 'application/octet-stream',
|
|
47
|
+
'content-range': 'bytes 0-3/2140453952',
|
|
48
|
+
etag: '"v1"',
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it("serves the bucket's style pointing at itself", async () => {
|
|
53
|
+
const { asked, proxy } = bucket(() => new Response(style, { headers: { 'content-type': 'application/json', etag: '"style"', 'last-modified': 'Tue, 19 Aug 2025 18:42:32 GMT' } }))
|
|
54
|
+
const answer = await proxy(new Request('http://localhost:8790/style.json'))
|
|
55
|
+
expect(asked[0]!.url).toBe(`${tilesBucket}/style.json`)
|
|
56
|
+
expect(JSON.parse(await answer.text()).sprite).toBe('http://localhost:8790/sprites/v4/light')
|
|
57
|
+
expect(answer.headers.get('etag')).toBeNull()
|
|
58
|
+
expect(answer.headers.get('last-modified')).toBeNull()
|
|
59
|
+
expect(answer.headers.get('cache-control')).toBe('no-cache')
|
|
60
|
+
expect(answer.headers.get('access-control-allow-origin')).toBe('*')
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it("passes on the bucket's errors as they are", async () => {
|
|
64
|
+
const { proxy } = bucket(() => new Response('missing', { status: 404 }))
|
|
65
|
+
const answer = await proxy(new Request('http://localhost:8790/style.json'))
|
|
66
|
+
expect(answer.status).toBe(404)
|
|
67
|
+
expect(await answer.text()).toBe('missing')
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('answers the CORS preflight itself, and only reads', async () => {
|
|
71
|
+
const { asked, proxy } = bucket(() => new Response('never'))
|
|
72
|
+
const preflight = await proxy(new Request('http://localhost:8790/20250819.pmtiles', { method: 'OPTIONS' }))
|
|
73
|
+
expect(preflight.status).toBe(204)
|
|
74
|
+
expect(preflight.headers.get('access-control-allow-methods')).toBe('GET, HEAD')
|
|
75
|
+
expect((await proxy(new Request('http://localhost:8790/style.json', { method: 'PUT', body: '{}' }))).status).toBe(405)
|
|
76
|
+
expect(asked).toEqual([])
|
|
77
|
+
})
|
|
78
|
+
})
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The development map tiles: the OpenMapTiles archive, sprites and fonts of the bucket below, which answers only its
|
|
3
|
+
* own sites' origins. Served through this proxy they come with CORS headers for any origin, and the bucket's style,
|
|
4
|
+
* which names the archive's old domain, names the proxy instead. The bucket is read, never written.
|
|
5
|
+
*/
|
|
6
|
+
export const tilesBucket = 'https://pub-aa7d484314e7429ebc2b0bcbc3c86bd8.r2.dev'
|
|
7
|
+
|
|
8
|
+
// The domain the bucket's style.json was written for, which no longer serves it.
|
|
9
|
+
const styleDomain = 'https://map.rentnudge.com'
|
|
10
|
+
|
|
11
|
+
// What a range read of the archive needs back: its part, and the ETag pmtiles checks it against.
|
|
12
|
+
const passedHeaders = ['content-type', 'content-range', 'accept-ranges', 'etag', 'last-modified', 'cache-control']
|
|
13
|
+
|
|
14
|
+
const cors = { 'access-control-allow-origin': '*', 'access-control-expose-headers': 'content-range, etag', 'access-control-allow-headers': 'range, if-match' }
|
|
15
|
+
|
|
16
|
+
// What the archive's data asks to be credited with, which its metadata leaves out.
|
|
17
|
+
const attribution = '<a href="https://openmaptiles.org/" target="_blank">© OpenMapTiles</a> <a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap contributors</a>'
|
|
18
|
+
|
|
19
|
+
type Style = { sources: Record<string, { attribution?: string }> }
|
|
20
|
+
|
|
21
|
+
/** The bucket's style with its sources, sprite and fonts at `origin`, each source credited. */
|
|
22
|
+
export const proxiedStyle = (text: string, origin: string) => {
|
|
23
|
+
const style = JSON.parse(text.replaceAll(styleDomain, origin)) as Style
|
|
24
|
+
for (const source of Object.values(style.sources)) source.attribution ??= attribution
|
|
25
|
+
return JSON.stringify(style)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Answers the bucket's files at the same paths, `/style.json` pointing at itself; `upstream` fetches from the bucket.
|
|
30
|
+
* Only reads: a GET or HEAD, with its `Range` passed on, and the CORS preflight that a range read can ask first.
|
|
31
|
+
*/
|
|
32
|
+
export const tilesProxy = (upstream: typeof fetch) => async (request: Request) => {
|
|
33
|
+
if (request.method === 'OPTIONS') return new Response(null, { status: 204, headers: { ...cors, 'access-control-allow-methods': 'GET, HEAD' } })
|
|
34
|
+
if (request.method !== 'GET' && request.method !== 'HEAD') return new Response('Only GET and HEAD', { status: 405, headers: cors })
|
|
35
|
+
const url = new URL(request.url)
|
|
36
|
+
const range = request.headers.get('range')
|
|
37
|
+
const answer = await upstream(`${tilesBucket}${url.pathname}`, { method: request.method, headers: range ? { range } : {} })
|
|
38
|
+
const headers = new Headers(cors)
|
|
39
|
+
for (const name of passedHeaders) {
|
|
40
|
+
const value = answer.headers.get(name)
|
|
41
|
+
if (value !== null) headers.set(name, value)
|
|
42
|
+
}
|
|
43
|
+
if (url.pathname !== '/style.json' || !answer.ok) return new Response(answer.body, { status: answer.status, headers })
|
|
44
|
+
// The style is the proxy's own text: it changes with the proxy, so a browser asks for it each time
|
|
45
|
+
for (const name of ['etag', 'last-modified']) headers.delete(name)
|
|
46
|
+
headers.set('cache-control', 'no-cache')
|
|
47
|
+
return new Response(proxiedStyle(await answer.text(), url.origin), { status: answer.status, headers })
|
|
48
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createServer } from 'node:http'
|
|
2
|
+
import { Readable } from 'node:stream'
|
|
3
|
+
import type { ReadableStream } from 'node:stream/web'
|
|
4
|
+
import { tilesProxy } from './tiles-proxy'
|
|
5
|
+
|
|
6
|
+
// `bun run map:tiles`: the development map tiles at http://localhost:8790, which PROTOBASE_MAP_STYLE_URL in .env names.
|
|
7
|
+
const port = Number(process.env.PORT ?? 8790)
|
|
8
|
+
const proxy = tilesProxy(fetch)
|
|
9
|
+
|
|
10
|
+
createServer(async (incoming, outgoing) => {
|
|
11
|
+
const headers = Object.entries(incoming.headers).flatMap(([name, value]): Array<[string, string]> => (typeof value === 'string' ? [[name, value]] : []))
|
|
12
|
+
const answer = await proxy(new Request(`http://localhost:${port}${incoming.url ?? '/'}`, { method: incoming.method ?? 'GET', headers }))
|
|
13
|
+
outgoing.writeHead(answer.status, Object.fromEntries(answer.headers))
|
|
14
|
+
if (!answer.body) return outgoing.end()
|
|
15
|
+
Readable.fromWeb(answer.body as ReadableStream).pipe(outgoing)
|
|
16
|
+
}).listen(port, () => console.log(`map tiles at http://localhost:${port}/style.json`))
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "property-portal",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"db:migrate": "bun db/migrate.ts",
|
|
8
|
+
"db:seed": "bun seed/cli.ts",
|
|
9
|
+
"db:reset": "bun db/drop.ts && bun db/migrate.ts && bun seed/cli.ts --scale small",
|
|
10
|
+
"map:tiles": "bun map/tiles.ts",
|
|
11
|
+
"typecheck": "tsc --noEmit",
|
|
12
|
+
"test": "vitest run",
|
|
13
|
+
"protobase": "protobase",
|
|
14
|
+
"build": "protobase build",
|
|
15
|
+
"serve": "protobase build && protobase serve dist",
|
|
16
|
+
"dev": "protobase dev"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"better-auth": "^1.7.12",
|
|
20
|
+
"jose": "^6.2.12",
|
|
21
|
+
"kysely": "^0.29.6",
|
|
22
|
+
"pg": "^8.23.1",
|
|
23
|
+
"postgres": "^3.4.9",
|
|
24
|
+
"react": "^19.3.0",
|
|
25
|
+
"react-email": "^6.11.1",
|
|
26
|
+
"sharp": "^0.35.5",
|
|
27
|
+
"zod": "^4.6.5"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@protobase/cli": "^0.53.7",
|
|
31
|
+
"@protobase/config": "^0.53.7",
|
|
32
|
+
"@protobase/layout": "^0.53.7",
|
|
33
|
+
"@protobase/schema": "^0.53.7",
|
|
34
|
+
"@protobase/server": "^0.53.7",
|
|
35
|
+
"@protobase/ui": "^0.53.7",
|
|
36
|
+
"@types/node": "^22.20.5",
|
|
37
|
+
"@types/pg": "^8.23.1",
|
|
38
|
+
"@types/react": "^19.3.0",
|
|
39
|
+
"typescript": "^6.0.3",
|
|
40
|
+
"vitest": "^5.0.3"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { defineConfig } from '@protobase/config'
|
|
2
|
+
|
|
3
|
+
// The whole portal, as one object. Its code is found by its path: resources in config/, roles in config/roles.ts, each
|
|
4
|
+
// job's run in jobs/<name>.ts and the auth emails' templates in emails/. Its address and secret are Protobase's
|
|
5
|
+
// defaults, PROTOBASE_URL and PROTOBASE_SECRET (see .env.example).
|
|
6
|
+
export default defineConfig({
|
|
7
|
+
auth: {
|
|
8
|
+
// Anyone makes their own account from the sign-in page, as a member, once a code mailed to their address proves it is
|
|
9
|
+
// theirs; admins are made on the host (`protobase users create --role admin`).
|
|
10
|
+
roles: ['admin', 'member'],
|
|
11
|
+
// `protobase users create` without `--role` makes a member too.
|
|
12
|
+
defaultRole: 'member',
|
|
13
|
+
signUp: { role: 'member' },
|
|
14
|
+
},
|
|
15
|
+
jobs: {
|
|
16
|
+
// Enqueued by Postgres when a listing is published (db/migrations/005_listing_published.sql).
|
|
17
|
+
'match-listing': {
|
|
18
|
+
description: 'Records an alert for each saved search a newly published listing fits, and mails those of searches that mail at once.',
|
|
19
|
+
arguments: { listingId: 'integer' },
|
|
20
|
+
},
|
|
21
|
+
'email-alerts': {
|
|
22
|
+
description: "Mails a member the alerts due for mail of their searches with this frequency, in one message, then marks them mailed.",
|
|
23
|
+
arguments: { userId: { type: 'text', min: 1 }, frequency: { type: 'enum', values: ['instant', 'daily'] } },
|
|
24
|
+
retryDelay: '1 minute',
|
|
25
|
+
},
|
|
26
|
+
'daily-alerts': {
|
|
27
|
+
description: 'Every morning at 7 in Amsterdam: a digest for each member whose daily searches found listings due for mail.',
|
|
28
|
+
schedule: '0 7 * * *',
|
|
29
|
+
timezone: 'Europe/Amsterdam',
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
// sharp makes the listing photos' variants and placeholders.
|
|
33
|
+
files: { images: 'sharp' },
|
|
34
|
+
// The maps draw PROTOBASE_MAP_STYLE_URL and switch to aerial photos.
|
|
35
|
+
maps: {
|
|
36
|
+
// The aerial photos the maps' switch offers, both open data under CC BY 4.0, free and without a key. Each covers its
|
|
37
|
+
// own country; elsewhere the default map shows through. The URLs and terms are in the README's map section.
|
|
38
|
+
layers: [
|
|
39
|
+
{
|
|
40
|
+
id: 'aerial_nl',
|
|
41
|
+
label: 'Aerial (NL)',
|
|
42
|
+
tiles: 'https://service.pdok.nl/hwh/luchtfotorgb/wmts/v1_0/Actueel_orthoHR/EPSG:3857/{z}/{x}/{y}.jpeg',
|
|
43
|
+
attribution: 'Luchtfoto <a href="https://www.beeldmateriaal.nl/">Beeldmateriaal Nederland</a>, CC BY 4.0, via <a href="https://www.pdok.nl/">PDOK</a>',
|
|
44
|
+
bounds: [3.2, 50.7, 7.3, 53.7],
|
|
45
|
+
maxzoom: 21,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
id: 'aerial_es',
|
|
49
|
+
label: 'Aerial (ES)',
|
|
50
|
+
tiles: 'https://www.ign.es/wmts/pnoa-ma?service=WMTS&request=GetTile&version=1.0.0&layer=OI.OrthoimageCoverage&style=default&format=image/jpeg&tilematrixset=GoogleMapsCompatible&tilematrix={z}&tilerow={y}&tilecol={x}',
|
|
51
|
+
attribution: '<a href="https://www.scne.es/">PNOA CC BY 4.0 scne.es</a>',
|
|
52
|
+
bounds: [-18.4, 27.5, 4.6, 44.0],
|
|
53
|
+
maxzoom: 20,
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
},
|
|
57
|
+
})
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { defineUi, type ActionContext } from '@protobase/ui'
|
|
2
|
+
|
|
3
|
+
// The signed-in member's favorite of the listing on the page, if they saved it: the API lists only their own.
|
|
4
|
+
const favoriteOf = async ({ record, client }: ActionContext) => {
|
|
5
|
+
const saved = await client.list('favorites', { filter: `listing_id = ${String(record!.id)}`, pageSize: 1 })
|
|
6
|
+
return saved.items[0]
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** "Save to favorites" on a listing: saves it once, however often it is pressed. */
|
|
10
|
+
const save_favorite = async (context: ActionContext) => {
|
|
11
|
+
if (await favoriteOf(context)) return
|
|
12
|
+
await context.client.create('favorites', { listing_id: context.record!.id })
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** "Remove from favorites" on a listing: takes it off, when it was saved. */
|
|
16
|
+
const remove_favorite = async (context: ActionContext) => {
|
|
17
|
+
const favorite = await favoriteOf(context)
|
|
18
|
+
if (favorite) await context.client.remove('favorites', String(favorite.id))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default defineUi({ actions: { save_favorite, remove_favorite } })
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { agencyNames } from '../data/names'
|
|
2
|
+
import type { PlaceSeed } from '../data/places'
|
|
3
|
+
import { slugify } from '../format'
|
|
4
|
+
import { int, pick, rngAt, weighted } from '../rng'
|
|
5
|
+
|
|
6
|
+
export type SeededAgency = { id: number; name: string; placeId: number; country: 'NL' | 'ES'; phone: string; email: string; website: string }
|
|
7
|
+
|
|
8
|
+
const phone = (country: 'NL' | 'ES', rand: () => number) =>
|
|
9
|
+
country === 'NL' ? `+31 ${int(rand, 10, 79)} ${int(rand, 100, 999)} ${int(rand, 1000, 9999)}` : `+34 9${int(rand, 10, 99)} ${int(rand, 100, 999)} ${int(rand, 100, 999)}`
|
|
10
|
+
|
|
11
|
+
/** Agency `ordinal`, its office in a place picked by how many listings the place draws. */
|
|
12
|
+
export const agencyAt = (places: PlaceSeed[], ordinal: number): SeededAgency => {
|
|
13
|
+
const rand = rngAt(301, ordinal)
|
|
14
|
+
const placeIndex = weighted(rand, places.map((place, index) => [index, place.weight] as const))
|
|
15
|
+
const place = places[placeIndex]!
|
|
16
|
+
const parts = agencyNames[place.country]
|
|
17
|
+
const base = place.country === 'NL' ? `${pick(rand, parts.first)} ${pick(rand, parts.second)}` : `${pick(rand, parts.first)} ${pick(rand, parts.second)}`
|
|
18
|
+
// Names repeat across places; the place keeps them apart, and the ordinal keeps their sites apart.
|
|
19
|
+
const name = `${base} ${place.name}`
|
|
20
|
+
const domain = `${slugify(base)}-${ordinal + 1}.${place.country === 'NL' ? 'nl' : 'es'}`
|
|
21
|
+
return { id: ordinal + 1, name, placeId: placeIndex + 1, country: place.country, phone: phone(place.country, rand), email: `info@${domain}`, website: `https://www.${domain}` }
|
|
22
|
+
}
|