1
2
3
4
5const CF_NOTICE = 'https://www.contractsfinder.service.gov.uk/Notice/';
6const FTS_NOTICE = 'https://www.find-tender.service.gov.uk/Notice/';
7
8
9const EMAIL = /[\p{L}\p{N}._%+-]+@[\p{L}\p{N}-]+(?:\.[\p{L}\p{N}-]+)+/gu;
10const PHONE = /(?:\+44[\s()-]*\d|\b0\d)(?:[\s()-]*\d){8,10}\b/g;
11export const scrub = (text) => (typeof text === 'string'
12 ? text.replace(EMAIL, '[email removed]').replace(PHONE, '[phone removed]')
13 : text ?? null);
14
15const uniq = (arr) => [...new Set(arr.filter((v) => v !== undefined && v !== null && v !== ''))];
16
17function noticeUrl(source, release) {
18 if (source === 'find-a-tender') return release.id ? FTS_NOTICE + release.id : null;
19 const docs = [
20 ...(release.tender?.documents ?? []),
21 ...(release.awards ?? []).flatMap((a) => a.documents ?? []),
22 ];
23 const own = docs.find((d) => typeof d.url === 'string' && d.url.startsWith(CF_NOTICE));
24 if (own) return own.url;
25
26 const guid = String(release.id ?? '').match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i);
27 return guid ? CF_NOTICE + guid[0] : null;
28}
29
30function pickValue(release) {
31 const t = release.tender ?? {};
32 const candidates = [
33 t.value,
34 t.minValue,
35 ...(t.lots ?? []).map((l) => l.value),
36 ...(release.awards ?? []).map((a) => a.value),
37 ].filter((v) => v && typeof v.amount === 'number' && Number.isFinite(v.amount));
38 if (!candidates.length) return { amount: null, currency: null };
39 return { amount: candidates[0].amount, currency: candidates[0].currency ?? 'GBP' };
40}
41
42export function normalise(source, release) {
43 const t = release.tender ?? {};
44 const parties = release.parties ?? [];
45 const buyerParty = parties.find((p) => (p.roles ?? []).includes('buyer'))
46 ?? parties.find((p) => p.id && p.id === release.buyer?.id)
47 ?? {};
48 const value = pickValue(release);
49 const awards = release.awards ?? [];
50
51 const cpv = [
52 t.classification,
53 ...(t.additionalClassifications ?? []),
54 ...(t.items ?? []).flatMap((i) => [i.classification, ...(i.additionalClassifications ?? [])]),
55 ].filter((c) => c && c.id && (!c.scheme || c.scheme === 'CPV'));
56
57 const regions = uniq((t.items ?? []).flatMap((i) => (i.deliveryAddresses ?? []).map((a) => a.region)));
58
59 const stage = Array.isArray(release.tag) ? release.tag.join(',') : (release.tag ?? null);
60
61 return {
62 source,
63 noticeId: release.id ?? null,
64 ocid: release.ocid ?? null,
65 stage,
66 status: t.status ?? null,
67 title: scrub(t.title),
68 description: scrub(t.description),
69 buyerName: release.buyer?.name ?? buyerParty.name ?? null,
70 buyerTown: buyerParty.address?.locality ?? null,
71 buyerPostcode: buyerParty.address?.postalCode ?? null,
72 buyerWebsite: buyerParty.details?.url ?? null,
73 valueAmount: value.amount,
74 valueCurrency: value.currency,
75 cpvCode: t.classification?.id ?? cpv[0]?.id ?? null,
76 cpvDescription: t.classification?.description ?? cpv[0]?.description ?? null,
77 allCpvCodes: uniq(cpv.map((c) => c.id)),
78 category: t.mainProcurementCategory ?? null,
79 regions,
80 procurementMethod: t.procurementMethodDetails ?? t.procurementMethod ?? null,
81 suitableForSme: t.suitability?.sme ?? null,
82 publishedDate: release.date ?? t.datePublished ?? null,
83 deadline: t.tenderPeriod?.endDate ?? null,
84 contractStart: t.contractPeriod?.startDate ?? awards[0]?.contractPeriod?.startDate ?? t.lots?.[0]?.contractPeriod?.startDate ?? null,
85 contractEnd: t.contractPeriod?.endDate ?? awards[0]?.contractPeriod?.endDate ?? t.lots?.[0]?.contractPeriod?.endDate ?? null,
86 awardedSuppliers: uniq(awards.flatMap((a) => (a.suppliers ?? []).map((s) => s.name))),
87 awardDate: awards[0]?.date ?? null,
88 noticeUrl: noticeUrl(source, release),
89 };
90}
91
92export function matches(row, f) {
93 if (f.keywords.length) {
94 const hay = `${row.title ?? ''} ${row.description ?? ''} ${row.cpvDescription ?? ''}`.toLowerCase();
95 if (!f.keywords.some((k) => hay.includes(k))) return false;
96 }
97 if (f.buyerContains && !(row.buyerName ?? '').toLowerCase().includes(f.buyerContains)) return false;
98 if (f.cpvPrefixes.length && !row.allCpvCodes.some((c) => f.cpvPrefixes.some((p) => String(c).startsWith(p)))) return false;
99 if (f.minValue !== null && !(row.valueAmount !== null && row.valueAmount >= f.minValue)) return false;
100 if (f.maxValue !== null && !(row.valueAmount !== null && row.valueAmount <= f.maxValue)) return false;
101 if (f.openOnly) {
102 if (!row.deadline) return false;
103 const d = Date.parse(row.deadline);
104 if (Number.isNaN(d) || d < f.now) return false;
105 }
106 return true;
107}