@bash-app/bash-common 30.225.0 → 30.226.0
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/__tests__/agePolicy.test.d.ts +2 -0
- package/dist/__tests__/agePolicy.test.d.ts.map +1 -0
- package/dist/__tests__/agePolicy.test.js +203 -0
- package/dist/__tests__/agePolicy.test.js.map +1 -0
- package/dist/__tests__/getDisplayGender.test.d.ts +2 -0
- package/dist/__tests__/getDisplayGender.test.d.ts.map +1 -0
- package/dist/__tests__/getDisplayGender.test.js +34 -0
- package/dist/__tests__/getDisplayGender.test.js.map +1 -0
- package/dist/__tests__/statusEnums.test.d.ts +2 -0
- package/dist/__tests__/statusEnums.test.d.ts.map +1 -0
- package/dist/__tests__/statusEnums.test.js +24 -0
- package/dist/__tests__/statusEnums.test.js.map +1 -0
- package/dist/aiApproval.d.ts +64 -0
- package/dist/aiApproval.d.ts.map +1 -0
- package/dist/aiApproval.js +54 -0
- package/dist/aiApproval.js.map +1 -0
- package/dist/definitions.d.ts +44 -2
- package/dist/definitions.d.ts.map +1 -1
- package/dist/definitions.js +106 -2
- package/dist/definitions.js.map +1 -1
- package/dist/extendedSchemas.d.ts +237 -104
- package/dist/extendedSchemas.d.ts.map +1 -1
- package/dist/extendedSchemas.js +12 -2
- package/dist/extendedSchemas.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/legalTemplates.d.ts +1 -1
- package/dist/legalTemplates.js +1 -1
- package/dist/utils/__tests__/badgeUtils.test.d.ts +2 -0
- package/dist/utils/__tests__/badgeUtils.test.d.ts.map +1 -0
- package/dist/utils/__tests__/badgeUtils.test.js +157 -0
- package/dist/utils/__tests__/badgeUtils.test.js.map +1 -0
- package/dist/utils/__tests__/bashPointsPaymentUtils.test.d.ts +2 -0
- package/dist/utils/__tests__/bashPointsPaymentUtils.test.d.ts.map +1 -0
- package/dist/utils/__tests__/bashPointsPaymentUtils.test.js +114 -0
- package/dist/utils/__tests__/bashPointsPaymentUtils.test.js.map +1 -0
- package/dist/utils/__tests__/contentFilterUtils.test.d.ts +2 -0
- package/dist/utils/__tests__/contentFilterUtils.test.d.ts.map +1 -0
- package/dist/utils/__tests__/contentFilterUtils.test.js +443 -0
- package/dist/utils/__tests__/contentFilterUtils.test.js.map +1 -0
- package/dist/utils/__tests__/displayDateUtils.test.d.ts +2 -0
- package/dist/utils/__tests__/displayDateUtils.test.d.ts.map +1 -0
- package/dist/utils/__tests__/displayDateUtils.test.js +105 -0
- package/dist/utils/__tests__/displayDateUtils.test.js.map +1 -0
- package/dist/utils/__tests__/getBackendHost.test.d.ts +2 -0
- package/dist/utils/__tests__/getBackendHost.test.d.ts.map +1 -0
- package/dist/utils/__tests__/getBackendHost.test.js +104 -0
- package/dist/utils/__tests__/getBackendHost.test.js.map +1 -0
- package/dist/utils/__tests__/getFrontendHost.test.d.ts +2 -0
- package/dist/utils/__tests__/getFrontendHost.test.d.ts.map +1 -0
- package/dist/utils/__tests__/getFrontendHost.test.js +207 -0
- package/dist/utils/__tests__/getFrontendHost.test.js.map +1 -0
- package/dist/utils/__tests__/promoCodesUtils.test.d.ts +2 -0
- package/dist/utils/__tests__/promoCodesUtils.test.d.ts.map +1 -0
- package/dist/utils/__tests__/promoCodesUtils.test.js +183 -0
- package/dist/utils/__tests__/promoCodesUtils.test.js.map +1 -0
- package/dist/utils/__tests__/qrCodeUtils.test.d.ts +2 -0
- package/dist/utils/__tests__/qrCodeUtils.test.d.ts.map +1 -0
- package/dist/utils/__tests__/qrCodeUtils.test.js +99 -0
- package/dist/utils/__tests__/qrCodeUtils.test.js.map +1 -0
- package/dist/utils/__tests__/stripeAccountUtils.test.d.ts +2 -0
- package/dist/utils/__tests__/stripeAccountUtils.test.d.ts.map +1 -0
- package/dist/utils/__tests__/stripeAccountUtils.test.js +75 -0
- package/dist/utils/__tests__/stripeAccountUtils.test.js.map +1 -0
- package/dist/utils/blog/__tests__/getRelatedBlogPosts.test.d.ts +2 -0
- package/dist/utils/blog/__tests__/getRelatedBlogPosts.test.d.ts.map +1 -0
- package/dist/utils/blog/__tests__/getRelatedBlogPosts.test.js +233 -0
- package/dist/utils/blog/__tests__/getRelatedBlogPosts.test.js.map +1 -0
- package/dist/utils/blog/blogCommentDbUtils.d.ts +161 -0
- package/dist/utils/blog/blogCommentDbUtils.d.ts.map +1 -0
- package/dist/utils/blog/blogCommentDbUtils.js +111 -0
- package/dist/utils/blog/blogCommentDbUtils.js.map +1 -0
- package/dist/utils/blog/blogDbUtils.d.ts +10 -0
- package/dist/utils/blog/blogDbUtils.d.ts.map +1 -1
- package/dist/utils/blog/blogDbUtils.js +71 -0
- package/dist/utils/blog/blogDbUtils.js.map +1 -1
- package/dist/utils/urlUtils.d.ts +10 -0
- package/dist/utils/urlUtils.d.ts.map +1 -1
- package/dist/utils/urlUtils.js +24 -5
- package/dist/utils/urlUtils.js.map +1 -1
- package/dist/utils/userUtils.d.ts +32 -0
- package/dist/utils/userUtils.d.ts.map +1 -1
- package/dist/utils/userUtils.js +27 -0
- package/dist/utils/userUtils.js.map +1 -1
- package/package.json +1 -1
- package/prisma/schema.prisma +927 -845
- package/src/__tests__/agePolicy.test.ts +257 -0
- package/src/__tests__/getDisplayGender.test.ts +50 -0
- package/src/__tests__/statusEnums.test.ts +36 -0
- package/src/definitions.ts +138 -1
- package/src/extendedSchemas.ts +41 -2
- package/src/index.ts +1 -0
- package/src/legalTemplates.ts +1 -1
- package/src/utils/__tests__/badgeUtils.test.ts +191 -0
- package/src/utils/__tests__/bashPointsPaymentUtils.test.ts +153 -0
- package/src/utils/__tests__/contentFilterUtils.test.ts +547 -0
- package/src/utils/__tests__/displayDateUtils.test.ts +113 -0
- package/src/utils/__tests__/getBackendHost.test.ts +128 -0
- package/src/utils/__tests__/getFrontendHost.test.ts +232 -0
- package/src/utils/__tests__/promoCodesUtils.test.ts +237 -0
- package/src/utils/__tests__/qrCodeUtils.test.ts +143 -0
- package/src/utils/__tests__/stripeAccountUtils.test.ts +93 -0
- package/src/utils/blog/__tests__/getRelatedBlogPosts.test.ts +271 -0
- package/src/utils/blog/blogCommentDbUtils.ts +161 -0
- package/src/utils/blog/blogDbUtils.ts +95 -1
- package/src/utils/urlUtils.ts +27 -5
- package/src/utils/userUtils.ts +40 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* qrCodeUtils — URL builders for the *navigable* `/ticket-details` deep
|
|
3
|
+
* links that get encoded into ticket QR codes (and createQrCodeImage that
|
|
4
|
+
* renders them as PNG data URLs).
|
|
5
|
+
*
|
|
6
|
+
* The contract these tests pin (consumed by the door-scan flow, ticket
|
|
7
|
+
* emails, and tier-share QR codes):
|
|
8
|
+
*
|
|
9
|
+
* bashEventPrismaCheckoutQrCodeUrl(eventId, checkoutId)
|
|
10
|
+
* → "/ticket-details/{eventId}/{checkoutId}"
|
|
11
|
+
*
|
|
12
|
+
* bashEventQrCodeUrl(eventId) → "/ticket-details/{eventId}"
|
|
13
|
+
* bashEventQrCodeUrl(eventId, tierId) → "/ticket-details/{eventId}/ticket-tier/{tierId}"
|
|
14
|
+
* bashEventQrCodeUrl(eventId, tierId, ticketId) → "/ticket-details/{eventId}/ticket/{ticketId}" (ticketId WINS over tierId)
|
|
15
|
+
* bashEventQrCodeUrl(eventId, _, _, slug) → "/ticket-details/{eventId}-{slug}" (slug version of the deep link)
|
|
16
|
+
*
|
|
17
|
+
* createQrCodeImage(data) → Promise<"data:image/png;base64,...">
|
|
18
|
+
*
|
|
19
|
+
* Bugs in any of these route door-scanners to the wrong page (= entry chaos).
|
|
20
|
+
*/
|
|
21
|
+
import {
|
|
22
|
+
bashEventPrismaCheckoutQrCodeUrl,
|
|
23
|
+
bashEventQrCodeUrl,
|
|
24
|
+
createQrCodeImage,
|
|
25
|
+
} from "../qrCodeUtils.js";
|
|
26
|
+
|
|
27
|
+
// ============================================================================
|
|
28
|
+
// bashEventPrismaCheckoutQrCodeUrl — checkout-id deep link
|
|
29
|
+
// ============================================================================
|
|
30
|
+
|
|
31
|
+
describe("bashEventPrismaCheckoutQrCodeUrl", () => {
|
|
32
|
+
it("composes /ticket-details/{eventId}/{checkoutId}", () => {
|
|
33
|
+
expect(bashEventPrismaCheckoutQrCodeUrl("evt_123", "co_456")).toBe(
|
|
34
|
+
"/ticket-details/evt_123/co_456"
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("does NOT throw on null/undefined inputs (downstream link is broken-but-safe)", () => {
|
|
39
|
+
// The current contract just stringifies via template literal, so we pin
|
|
40
|
+
// the literal output rather than asserting an exception.
|
|
41
|
+
expect(bashEventPrismaCheckoutQrCodeUrl(null, null)).toBe(
|
|
42
|
+
"/ticket-details/null/null"
|
|
43
|
+
);
|
|
44
|
+
expect(
|
|
45
|
+
bashEventPrismaCheckoutQrCodeUrl(undefined, undefined)
|
|
46
|
+
).toBe("/ticket-details/undefined/undefined");
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// ============================================================================
|
|
51
|
+
// bashEventQrCodeUrl — base / tier / ticket / slug variants
|
|
52
|
+
// ============================================================================
|
|
53
|
+
|
|
54
|
+
describe("bashEventQrCodeUrl", () => {
|
|
55
|
+
describe("base url (no tier, no ticket, no slug)", () => {
|
|
56
|
+
it("returns /ticket-details/{eventId}", () => {
|
|
57
|
+
expect(bashEventQrCodeUrl("evt_1")).toBe("/ticket-details/evt_1");
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe("with ticketTierId", () => {
|
|
62
|
+
it("appends /ticket-tier/{tierId}", () => {
|
|
63
|
+
expect(bashEventQrCodeUrl("evt_1", "tier_a")).toBe(
|
|
64
|
+
"/ticket-details/evt_1/ticket-tier/tier_a"
|
|
65
|
+
);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("ignores null ticketId in favor of tier", () => {
|
|
69
|
+
expect(bashEventQrCodeUrl("evt_1", "tier_a", null)).toBe(
|
|
70
|
+
"/ticket-details/evt_1/ticket-tier/tier_a"
|
|
71
|
+
);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("with ticketId (the door-scan link)", () => {
|
|
76
|
+
it("appends /ticket/{ticketId}", () => {
|
|
77
|
+
expect(
|
|
78
|
+
bashEventQrCodeUrl("evt_1", undefined, "ticket_xyz")
|
|
79
|
+
).toBe("/ticket-details/evt_1/ticket/ticket_xyz");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("PREFERS ticketId over ticketTierId when both are provided", () => {
|
|
83
|
+
// Tickets are more specific than tiers; the deep link must resolve to the
|
|
84
|
+
// individual ticket (otherwise the door scanner can't validate redemption).
|
|
85
|
+
expect(
|
|
86
|
+
bashEventQrCodeUrl("evt_1", "tier_a", "ticket_xyz")
|
|
87
|
+
).toBe("/ticket-details/evt_1/ticket/ticket_xyz");
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe("with slug", () => {
|
|
92
|
+
it("uses the slugged base (`/ticket-details/{eventId}-{slug}`)", () => {
|
|
93
|
+
expect(
|
|
94
|
+
bashEventQrCodeUrl("evt_1", undefined, undefined, "summer-rave")
|
|
95
|
+
).toBe("/ticket-details/evt_1-summer-rave");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("appends ticketId on the slugged base", () => {
|
|
99
|
+
expect(
|
|
100
|
+
bashEventQrCodeUrl("evt_1", undefined, "ticket_xyz", "summer-rave")
|
|
101
|
+
).toBe("/ticket-details/evt_1-summer-rave/ticket/ticket_xyz");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("appends ticket-tier on the slugged base when no ticketId", () => {
|
|
105
|
+
expect(
|
|
106
|
+
bashEventQrCodeUrl("evt_1", "tier_a", undefined, "summer-rave")
|
|
107
|
+
).toBe("/ticket-details/evt_1-summer-rave/ticket-tier/tier_a");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("falls back to non-slug base when slug is null", () => {
|
|
111
|
+
expect(bashEventQrCodeUrl("evt_1", null, null, null)).toBe(
|
|
112
|
+
"/ticket-details/evt_1"
|
|
113
|
+
);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// ============================================================================
|
|
119
|
+
// createQrCodeImage — PNG data URL
|
|
120
|
+
// ============================================================================
|
|
121
|
+
|
|
122
|
+
describe("createQrCodeImage", () => {
|
|
123
|
+
it("returns a base64-encoded PNG data URL", async () => {
|
|
124
|
+
const dataUrl = await createQrCodeImage(
|
|
125
|
+
"https://bash.app/ticket-details/evt_1/ticket/abc"
|
|
126
|
+
);
|
|
127
|
+
expect(dataUrl).toMatch(/^data:image\/png;base64,/);
|
|
128
|
+
// Sanity: a real QR image will have *meaningful* base64 length, not a few bytes.
|
|
129
|
+
expect(dataUrl.length).toBeGreaterThan(200);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("produces a deterministic PNG for the same input (no nonce / timestamp leakage)", async () => {
|
|
133
|
+
const a = await createQrCodeImage("ticket://abc");
|
|
134
|
+
const b = await createQrCodeImage("ticket://abc");
|
|
135
|
+
expect(a).toBe(b);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("produces DIFFERENT output for different payloads", async () => {
|
|
139
|
+
const a = await createQrCodeImage("ticket://abc");
|
|
140
|
+
const b = await createQrCodeImage("ticket://xyz");
|
|
141
|
+
expect(a).not.toBe(b);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* stripeAccountUtils — the gate that decides whether a host can accept money.
|
|
3
|
+
*
|
|
4
|
+
* `stripeAccountFullyLinked` returns `true` ONLY when ALL FOUR of these are true:
|
|
5
|
+
* - linkingStatus is non-null
|
|
6
|
+
* - hasOutstandingRequirements === false
|
|
7
|
+
* - isOnboarded === true
|
|
8
|
+
* - paymentsEnabled === true
|
|
9
|
+
* - taxMonitoringEnabled === true
|
|
10
|
+
*
|
|
11
|
+
* This is the single check used everywhere we decide "can this host get paid?"
|
|
12
|
+
* Returning `true` when any one is false leads to checkouts that can't actually
|
|
13
|
+
* disburse money. We pin all 5 axes individually so a future refactor that
|
|
14
|
+
* accidentally drops one will fail loudly.
|
|
15
|
+
*/
|
|
16
|
+
import { stripeAccountFullyLinked } from "../stripeAccountUtils.js";
|
|
17
|
+
import type { StripeLinkingStatus } from "../../definitions.js";
|
|
18
|
+
|
|
19
|
+
const fullyLinked: StripeLinkingStatus = {
|
|
20
|
+
isOnboarded: true,
|
|
21
|
+
hasOutstandingRequirements: false,
|
|
22
|
+
paymentsEnabled: true,
|
|
23
|
+
taxMonitoringEnabled: true,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
describe("stripeAccountFullyLinked", () => {
|
|
27
|
+
describe("nullish guard", () => {
|
|
28
|
+
it("returns false for null", () => {
|
|
29
|
+
expect(stripeAccountFullyLinked(null)).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
it("returns false for undefined", () => {
|
|
32
|
+
expect(stripeAccountFullyLinked(undefined)).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe("happy path", () => {
|
|
37
|
+
it("returns true when ALL four flags are correct", () => {
|
|
38
|
+
expect(stripeAccountFullyLinked(fullyLinked)).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe("each individual gate must be satisfied", () => {
|
|
43
|
+
it("returns false when isOnboarded is false", () => {
|
|
44
|
+
expect(
|
|
45
|
+
stripeAccountFullyLinked({ ...fullyLinked, isOnboarded: false })
|
|
46
|
+
).toBe(false);
|
|
47
|
+
});
|
|
48
|
+
it("returns false when hasOutstandingRequirements is true", () => {
|
|
49
|
+
expect(
|
|
50
|
+
stripeAccountFullyLinked({
|
|
51
|
+
...fullyLinked,
|
|
52
|
+
hasOutstandingRequirements: true,
|
|
53
|
+
})
|
|
54
|
+
).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
it("returns false when paymentsEnabled is false", () => {
|
|
57
|
+
expect(
|
|
58
|
+
stripeAccountFullyLinked({ ...fullyLinked, paymentsEnabled: false })
|
|
59
|
+
).toBe(false);
|
|
60
|
+
});
|
|
61
|
+
it("returns false when taxMonitoringEnabled is false", () => {
|
|
62
|
+
expect(
|
|
63
|
+
stripeAccountFullyLinked({
|
|
64
|
+
...fullyLinked,
|
|
65
|
+
taxMonitoringEnabled: false,
|
|
66
|
+
})
|
|
67
|
+
).toBe(false);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe("multi-flag failures (any combination = false)", () => {
|
|
72
|
+
it("returns false when payments AND tax monitoring are both disabled", () => {
|
|
73
|
+
expect(
|
|
74
|
+
stripeAccountFullyLinked({
|
|
75
|
+
...fullyLinked,
|
|
76
|
+
paymentsEnabled: false,
|
|
77
|
+
taxMonitoringEnabled: false,
|
|
78
|
+
})
|
|
79
|
+
).toBe(false);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("returns false when EVERY flag is in the wrong state", () => {
|
|
83
|
+
expect(
|
|
84
|
+
stripeAccountFullyLinked({
|
|
85
|
+
isOnboarded: false,
|
|
86
|
+
hasOutstandingRequirements: true,
|
|
87
|
+
paymentsEnabled: false,
|
|
88
|
+
taxMonitoringEnabled: false,
|
|
89
|
+
})
|
|
90
|
+
).toBe(false);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
// Tests for getRelatedBlogPosts -- the tag-overlap-first scoring algorithm
|
|
2
|
+
// that powers the "Related reading" rail under every blog detail page. The
|
|
3
|
+
// scoring function is the SEO-critical surface (controls the internal-link
|
|
4
|
+
// graph) so we exercise every branch of the priority sort:
|
|
5
|
+
// 1. tag overlap count (descending)
|
|
6
|
+
// 2. shared category (1/0)
|
|
7
|
+
// 3. recency (most recent first)
|
|
8
|
+
//
|
|
9
|
+
// Plus the edge cases:
|
|
10
|
+
// - source post does not exist -> []
|
|
11
|
+
// - source has no tags + no category -> still returns recent published posts
|
|
12
|
+
// - limit cap
|
|
13
|
+
// - publishedAt null on a candidate is treated as oldest
|
|
14
|
+
|
|
15
|
+
import { BlogStatus } from "@prisma/client";
|
|
16
|
+
import { getRelatedBlogPosts } from "../blogDbUtils.js";
|
|
17
|
+
|
|
18
|
+
interface SourceShape {
|
|
19
|
+
id: string;
|
|
20
|
+
categoryId: string | null;
|
|
21
|
+
tags: { id: string }[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface CandidateShape {
|
|
25
|
+
id: string;
|
|
26
|
+
slug: string | null;
|
|
27
|
+
title: string;
|
|
28
|
+
excerpt: string | null;
|
|
29
|
+
coverImage: string | null;
|
|
30
|
+
readTime: number | null;
|
|
31
|
+
publishedAt: Date | null;
|
|
32
|
+
categoryId: string | null;
|
|
33
|
+
tags: { id: string }[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function makePrismaMock(opts: {
|
|
37
|
+
source: SourceShape | null;
|
|
38
|
+
candidates: CandidateShape[];
|
|
39
|
+
}) {
|
|
40
|
+
const findUnique = jest.fn().mockResolvedValue(opts.source);
|
|
41
|
+
const findMany = jest.fn().mockResolvedValue(opts.candidates);
|
|
42
|
+
return {
|
|
43
|
+
blogPost: { findUnique, findMany },
|
|
44
|
+
__findUnique: findUnique,
|
|
45
|
+
__findMany: findMany,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function candidate(overrides: Partial<CandidateShape> = {}): CandidateShape {
|
|
50
|
+
return {
|
|
51
|
+
id: "c-default",
|
|
52
|
+
slug: "c-default",
|
|
53
|
+
title: "Default Candidate",
|
|
54
|
+
excerpt: "default",
|
|
55
|
+
coverImage: null,
|
|
56
|
+
readTime: 5,
|
|
57
|
+
publishedAt: new Date("2026-01-01T00:00:00Z"),
|
|
58
|
+
categoryId: null,
|
|
59
|
+
tags: [],
|
|
60
|
+
...overrides,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
describe("getRelatedBlogPosts", () => {
|
|
65
|
+
it("returns [] when the source post does not exist", async () => {
|
|
66
|
+
const prisma = makePrismaMock({ source: null, candidates: [] });
|
|
67
|
+
const out = await getRelatedBlogPosts(prisma as any, "missing-id");
|
|
68
|
+
expect(out).toEqual([]);
|
|
69
|
+
expect(prisma.__findMany).not.toHaveBeenCalled();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("ranks candidates by shared tag count (descending)", async () => {
|
|
73
|
+
const source: SourceShape = {
|
|
74
|
+
id: "src",
|
|
75
|
+
categoryId: null,
|
|
76
|
+
tags: [{ id: "t1" }, { id: "t2" }, { id: "t3" }],
|
|
77
|
+
};
|
|
78
|
+
const candidates: CandidateShape[] = [
|
|
79
|
+
candidate({
|
|
80
|
+
id: "low",
|
|
81
|
+
title: "One tag in common",
|
|
82
|
+
tags: [{ id: "t1" }],
|
|
83
|
+
publishedAt: new Date("2026-03-01"),
|
|
84
|
+
}),
|
|
85
|
+
candidate({
|
|
86
|
+
id: "high",
|
|
87
|
+
title: "Three tags in common",
|
|
88
|
+
tags: [{ id: "t1" }, { id: "t2" }, { id: "t3" }],
|
|
89
|
+
publishedAt: new Date("2026-01-01"),
|
|
90
|
+
}),
|
|
91
|
+
candidate({
|
|
92
|
+
id: "mid",
|
|
93
|
+
title: "Two tags in common",
|
|
94
|
+
tags: [{ id: "t1" }, { id: "t2" }],
|
|
95
|
+
publishedAt: new Date("2026-02-01"),
|
|
96
|
+
}),
|
|
97
|
+
];
|
|
98
|
+
const prisma = makePrismaMock({ source, candidates });
|
|
99
|
+
const out = await getRelatedBlogPosts(prisma as any, "src", 4);
|
|
100
|
+
expect(out.map((p) => p.id)).toEqual(["high", "mid", "low"]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("uses shared category as the tiebreaker when tag counts tie", async () => {
|
|
104
|
+
const source: SourceShape = {
|
|
105
|
+
id: "src",
|
|
106
|
+
categoryId: "cat-1",
|
|
107
|
+
tags: [{ id: "t1" }],
|
|
108
|
+
};
|
|
109
|
+
const candidates: CandidateShape[] = [
|
|
110
|
+
candidate({
|
|
111
|
+
id: "without-category",
|
|
112
|
+
tags: [{ id: "t1" }],
|
|
113
|
+
categoryId: "cat-other",
|
|
114
|
+
publishedAt: new Date("2026-03-01"),
|
|
115
|
+
}),
|
|
116
|
+
candidate({
|
|
117
|
+
id: "with-category",
|
|
118
|
+
tags: [{ id: "t1" }],
|
|
119
|
+
categoryId: "cat-1",
|
|
120
|
+
publishedAt: new Date("2026-01-01"),
|
|
121
|
+
}),
|
|
122
|
+
];
|
|
123
|
+
const prisma = makePrismaMock({ source, candidates });
|
|
124
|
+
const out = await getRelatedBlogPosts(prisma as any, "src", 4);
|
|
125
|
+
expect(out.map((p) => p.id)).toEqual(["with-category", "without-category"]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("falls back to recency when both tag count and category tie", async () => {
|
|
129
|
+
const source: SourceShape = {
|
|
130
|
+
id: "src",
|
|
131
|
+
categoryId: "cat-1",
|
|
132
|
+
tags: [{ id: "t1" }],
|
|
133
|
+
};
|
|
134
|
+
const candidates: CandidateShape[] = [
|
|
135
|
+
candidate({
|
|
136
|
+
id: "older",
|
|
137
|
+
tags: [{ id: "t1" }],
|
|
138
|
+
categoryId: "cat-1",
|
|
139
|
+
publishedAt: new Date("2026-01-01"),
|
|
140
|
+
}),
|
|
141
|
+
candidate({
|
|
142
|
+
id: "newer",
|
|
143
|
+
tags: [{ id: "t1" }],
|
|
144
|
+
categoryId: "cat-1",
|
|
145
|
+
publishedAt: new Date("2026-04-01"),
|
|
146
|
+
}),
|
|
147
|
+
];
|
|
148
|
+
const prisma = makePrismaMock({ source, candidates });
|
|
149
|
+
const out = await getRelatedBlogPosts(prisma as any, "src", 4);
|
|
150
|
+
expect(out.map((p) => p.id)).toEqual(["newer", "older"]);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("treats null publishedAt as the oldest possible candidate", async () => {
|
|
154
|
+
const source: SourceShape = {
|
|
155
|
+
id: "src",
|
|
156
|
+
categoryId: null,
|
|
157
|
+
tags: [{ id: "t1" }],
|
|
158
|
+
};
|
|
159
|
+
const candidates: CandidateShape[] = [
|
|
160
|
+
candidate({
|
|
161
|
+
id: "no-date",
|
|
162
|
+
tags: [{ id: "t1" }],
|
|
163
|
+
publishedAt: null,
|
|
164
|
+
}),
|
|
165
|
+
candidate({
|
|
166
|
+
id: "dated",
|
|
167
|
+
tags: [{ id: "t1" }],
|
|
168
|
+
publishedAt: new Date("2026-01-01"),
|
|
169
|
+
}),
|
|
170
|
+
];
|
|
171
|
+
const prisma = makePrismaMock({ source, candidates });
|
|
172
|
+
const out = await getRelatedBlogPosts(prisma as any, "src", 4);
|
|
173
|
+
expect(out.map((p) => p.id)).toEqual(["dated", "no-date"]);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("respects the limit parameter", async () => {
|
|
177
|
+
const source: SourceShape = {
|
|
178
|
+
id: "src",
|
|
179
|
+
categoryId: null,
|
|
180
|
+
tags: [{ id: "t1" }],
|
|
181
|
+
};
|
|
182
|
+
const candidates: CandidateShape[] = Array.from({ length: 8 }, (_, i) =>
|
|
183
|
+
candidate({
|
|
184
|
+
id: `c${i}`,
|
|
185
|
+
title: `Candidate ${i}`,
|
|
186
|
+
tags: [{ id: "t1" }],
|
|
187
|
+
publishedAt: new Date(2026, 0, i + 1),
|
|
188
|
+
})
|
|
189
|
+
);
|
|
190
|
+
const prisma = makePrismaMock({ source, candidates });
|
|
191
|
+
const out = await getRelatedBlogPosts(prisma as any, "src", 3);
|
|
192
|
+
expect(out).toHaveLength(3);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("defaults the limit to 4 when not provided", async () => {
|
|
196
|
+
const source: SourceShape = {
|
|
197
|
+
id: "src",
|
|
198
|
+
categoryId: null,
|
|
199
|
+
tags: [{ id: "t1" }],
|
|
200
|
+
};
|
|
201
|
+
const candidates: CandidateShape[] = Array.from({ length: 10 }, (_, i) =>
|
|
202
|
+
candidate({ id: `c${i}`, tags: [{ id: "t1" }] })
|
|
203
|
+
);
|
|
204
|
+
const prisma = makePrismaMock({ source, candidates });
|
|
205
|
+
const out = await getRelatedBlogPosts(prisma as any, "src");
|
|
206
|
+
expect(out).toHaveLength(4);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("queries by tag-overlap OR shared category, excluding the source", async () => {
|
|
210
|
+
const source: SourceShape = {
|
|
211
|
+
id: "src",
|
|
212
|
+
categoryId: "cat-1",
|
|
213
|
+
tags: [{ id: "t1" }, { id: "t2" }],
|
|
214
|
+
};
|
|
215
|
+
const prisma = makePrismaMock({ source, candidates: [] });
|
|
216
|
+
await getRelatedBlogPosts(prisma as any, "src", 5);
|
|
217
|
+
|
|
218
|
+
const where = prisma.__findMany.mock.calls[0][0].where;
|
|
219
|
+
expect(where.id).toEqual({ not: "src" });
|
|
220
|
+
expect(where.status).toBe(BlogStatus.Published);
|
|
221
|
+
expect(where.publishedAt).toEqual({ not: null });
|
|
222
|
+
expect(where.OR).toEqual([
|
|
223
|
+
{ tags: { some: { id: { in: ["t1", "t2"] } } } },
|
|
224
|
+
{ categoryId: "cat-1" },
|
|
225
|
+
]);
|
|
226
|
+
// candidate pool size is limit * 4 to give the scorer headroom
|
|
227
|
+
expect(prisma.__findMany.mock.calls[0][0].take).toBe(20);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("omits the OR clause entirely when source has no tags and no category", async () => {
|
|
231
|
+
const source: SourceShape = { id: "src", categoryId: null, tags: [] };
|
|
232
|
+
const prisma = makePrismaMock({ source, candidates: [] });
|
|
233
|
+
await getRelatedBlogPosts(prisma as any, "src");
|
|
234
|
+
const where = prisma.__findMany.mock.calls[0][0].where;
|
|
235
|
+
expect(where.OR).toBeUndefined();
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("returns the trimmed RelatedBlogPost shape (no internal scoring fields)", async () => {
|
|
239
|
+
const source: SourceShape = {
|
|
240
|
+
id: "src",
|
|
241
|
+
categoryId: null,
|
|
242
|
+
tags: [{ id: "t1" }],
|
|
243
|
+
};
|
|
244
|
+
const candidates: CandidateShape[] = [
|
|
245
|
+
candidate({
|
|
246
|
+
id: "c1",
|
|
247
|
+
slug: "c1-slug",
|
|
248
|
+
title: "C1",
|
|
249
|
+
excerpt: "ex",
|
|
250
|
+
coverImage: "img.png",
|
|
251
|
+
readTime: 7,
|
|
252
|
+
publishedAt: new Date("2026-01-01"),
|
|
253
|
+
tags: [{ id: "t1" }],
|
|
254
|
+
categoryId: "cat-1",
|
|
255
|
+
}),
|
|
256
|
+
];
|
|
257
|
+
const prisma = makePrismaMock({ source, candidates });
|
|
258
|
+
const out = await getRelatedBlogPosts(prisma as any, "src", 4);
|
|
259
|
+
expect(out[0]).toEqual({
|
|
260
|
+
id: "c1",
|
|
261
|
+
slug: "c1-slug",
|
|
262
|
+
title: "C1",
|
|
263
|
+
excerpt: "ex",
|
|
264
|
+
coverImage: "img.png",
|
|
265
|
+
readTime: 7,
|
|
266
|
+
publishedAt: new Date("2026-01-01"),
|
|
267
|
+
});
|
|
268
|
+
expect(Object.keys(out[0])).not.toContain("categoryId");
|
|
269
|
+
expect(Object.keys(out[0])).not.toContain("tags");
|
|
270
|
+
});
|
|
271
|
+
});
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { PrismaClient, BlogComment, CommentStatus, Prisma } from '@prisma/client';
|
|
2
|
+
import { FRONT_END_USER_DATA_TO_SELECT, PublicUser } from '../../extendedSchemas.js';
|
|
3
|
+
|
|
4
|
+
// Shape returned by every helper below. Mirrors INVITATION_DATA_TO_INCLUDE
|
|
5
|
+
// pattern so consumers never have to `as`-cast.
|
|
6
|
+
export const BLOG_COMMENT_INCLUDE = {
|
|
7
|
+
author: {
|
|
8
|
+
select: FRONT_END_USER_DATA_TO_SELECT,
|
|
9
|
+
},
|
|
10
|
+
replies: {
|
|
11
|
+
where: { status: CommentStatus.Published },
|
|
12
|
+
orderBy: { createdAt: 'asc' },
|
|
13
|
+
include: {
|
|
14
|
+
author: {
|
|
15
|
+
select: FRONT_END_USER_DATA_TO_SELECT,
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
} satisfies Prisma.BlogCommentInclude;
|
|
20
|
+
|
|
21
|
+
export interface BlogCommentWithAuthor extends BlogComment {
|
|
22
|
+
author: PublicUser;
|
|
23
|
+
replies?: (BlogComment & { author: PublicUser })[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const MAX_COMMENT_LENGTH = 4000;
|
|
27
|
+
const MIN_COMMENT_LENGTH = 1;
|
|
28
|
+
|
|
29
|
+
export function validateCommentContent(content: string): { ok: true } | { ok: false; reason: string } {
|
|
30
|
+
const trimmed = content.trim();
|
|
31
|
+
if (trimmed.length < MIN_COMMENT_LENGTH) {
|
|
32
|
+
return { ok: false, reason: 'Comment cannot be empty.' };
|
|
33
|
+
}
|
|
34
|
+
if (trimmed.length > MAX_COMMENT_LENGTH) {
|
|
35
|
+
return { ok: false, reason: `Comment is too long (max ${MAX_COMMENT_LENGTH} characters).` };
|
|
36
|
+
}
|
|
37
|
+
return { ok: true };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Public list: only Published comments + their Published replies.
|
|
41
|
+
export async function getBlogCommentsForPost(
|
|
42
|
+
prisma: PrismaClient,
|
|
43
|
+
postId: string
|
|
44
|
+
): Promise<BlogCommentWithAuthor[]> {
|
|
45
|
+
const comments = await prisma.blogComment.findMany({
|
|
46
|
+
where: {
|
|
47
|
+
postId,
|
|
48
|
+
parentId: null,
|
|
49
|
+
status: CommentStatus.Published,
|
|
50
|
+
},
|
|
51
|
+
include: BLOG_COMMENT_INCLUDE,
|
|
52
|
+
orderBy: { createdAt: 'desc' },
|
|
53
|
+
});
|
|
54
|
+
return comments;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Admin list: any status, used by the moderation queue.
|
|
58
|
+
export async function getBlogCommentsByStatus(
|
|
59
|
+
prisma: PrismaClient,
|
|
60
|
+
status: CommentStatus | CommentStatus[],
|
|
61
|
+
options: { limit?: number; offset?: number } = {}
|
|
62
|
+
): Promise<BlogCommentWithAuthor[]> {
|
|
63
|
+
const { limit = 50, offset = 0 } = options;
|
|
64
|
+
const statusFilter = Array.isArray(status) ? { in: status } : status;
|
|
65
|
+
const comments = await prisma.blogComment.findMany({
|
|
66
|
+
where: { status: statusFilter },
|
|
67
|
+
include: BLOG_COMMENT_INCLUDE,
|
|
68
|
+
orderBy: { createdAt: 'desc' },
|
|
69
|
+
skip: offset,
|
|
70
|
+
take: limit,
|
|
71
|
+
});
|
|
72
|
+
return comments;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function getBlogCommentById(
|
|
76
|
+
prisma: PrismaClient,
|
|
77
|
+
id: string
|
|
78
|
+
): Promise<BlogCommentWithAuthor | null> {
|
|
79
|
+
return prisma.blogComment.findUnique({
|
|
80
|
+
where: { id },
|
|
81
|
+
include: BLOG_COMMENT_INCLUDE,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Caller (the API endpoint) is responsible for running AI moderation BEFORE
|
|
86
|
+
// calling this and choosing the resulting `status` (Published vs Pending).
|
|
87
|
+
// Keeping moderation in the API layer avoids dragging the OpenAI dependency
|
|
88
|
+
// into bash-common.
|
|
89
|
+
export async function createBlogComment(
|
|
90
|
+
prisma: PrismaClient,
|
|
91
|
+
data: {
|
|
92
|
+
postId: string;
|
|
93
|
+
authorId: string;
|
|
94
|
+
parentId?: string | null;
|
|
95
|
+
content: string;
|
|
96
|
+
status: CommentStatus;
|
|
97
|
+
}
|
|
98
|
+
): Promise<BlogCommentWithAuthor> {
|
|
99
|
+
return prisma.blogComment.create({
|
|
100
|
+
data: {
|
|
101
|
+
postId: data.postId,
|
|
102
|
+
authorId: data.authorId,
|
|
103
|
+
parentId: data.parentId ?? null,
|
|
104
|
+
content: data.content.trim(),
|
|
105
|
+
status: data.status,
|
|
106
|
+
},
|
|
107
|
+
include: BLOG_COMMENT_INCLUDE,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export async function updateBlogCommentContent(
|
|
112
|
+
prisma: PrismaClient,
|
|
113
|
+
id: string,
|
|
114
|
+
data: { content: string; status?: CommentStatus }
|
|
115
|
+
): Promise<BlogCommentWithAuthor> {
|
|
116
|
+
return prisma.blogComment.update({
|
|
117
|
+
where: { id },
|
|
118
|
+
data: {
|
|
119
|
+
content: data.content.trim(),
|
|
120
|
+
...(data.status ? { status: data.status } : {}),
|
|
121
|
+
},
|
|
122
|
+
include: BLOG_COMMENT_INCLUDE,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export async function setBlogCommentStatus(
|
|
127
|
+
prisma: PrismaClient,
|
|
128
|
+
id: string,
|
|
129
|
+
status: CommentStatus
|
|
130
|
+
): Promise<BlogCommentWithAuthor> {
|
|
131
|
+
return prisma.blogComment.update({
|
|
132
|
+
where: { id },
|
|
133
|
+
data: { status },
|
|
134
|
+
include: BLOG_COMMENT_INCLUDE,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Soft delete: preserves the row (and any child replies) so threading stays
|
|
139
|
+
// intact, but hides it from public reads. Use `setBlogCommentStatus` with
|
|
140
|
+
// `Trash` rather than a hard delete unless an admin explicitly removes it.
|
|
141
|
+
export async function softDeleteBlogComment(
|
|
142
|
+
prisma: PrismaClient,
|
|
143
|
+
id: string
|
|
144
|
+
): Promise<BlogCommentWithAuthor> {
|
|
145
|
+
return setBlogCommentStatus(prisma, id, CommentStatus.Trash);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export async function countRecentCommentsByAuthor(
|
|
149
|
+
prisma: PrismaClient,
|
|
150
|
+
authorId: string,
|
|
151
|
+
windowMs: number
|
|
152
|
+
): Promise<number> {
|
|
153
|
+
const since = new Date(Date.now() - windowMs);
|
|
154
|
+
return prisma.blogComment.count({
|
|
155
|
+
where: {
|
|
156
|
+
authorId,
|
|
157
|
+
createdAt: { gte: since },
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|