@bash-app/bash-common 30.442.0 → 30.444.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/extendedSchemas.d.ts +87 -224
- package/dist/extendedSchemas.d.ts.map +1 -1
- package/dist/extendedSchemas.js +12 -7
- package/dist/extendedSchemas.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/membershipDefinitions.js +2 -2
- package/dist/membershipDefinitions.js.map +1 -1
- package/dist/mirroredPrismaEnums.d.ts +1 -0
- package/dist/mirroredPrismaEnums.d.ts.map +1 -1
- package/dist/mirroredPrismaEnums.js +1 -0
- package/dist/mirroredPrismaEnums.js.map +1 -1
- package/dist/utils/__tests__/eventTaskVolunteerRewardUtils.test.d.ts +2 -0
- package/dist/utils/__tests__/eventTaskVolunteerRewardUtils.test.d.ts.map +1 -0
- package/dist/utils/__tests__/eventTaskVolunteerRewardUtils.test.js +41 -0
- package/dist/utils/__tests__/eventTaskVolunteerRewardUtils.test.js.map +1 -0
- package/dist/utils/eventTaskVolunteerRewardUtils.d.ts +61 -0
- package/dist/utils/eventTaskVolunteerRewardUtils.d.ts.map +1 -0
- package/dist/utils/eventTaskVolunteerRewardUtils.js +152 -0
- package/dist/utils/eventTaskVolunteerRewardUtils.js.map +1 -0
- package/dist/utils/organizationMembershipValidation.d.ts +7 -0
- package/dist/utils/organizationMembershipValidation.d.ts.map +1 -0
- package/dist/utils/organizationMembershipValidation.js +16 -0
- package/dist/utils/organizationMembershipValidation.js.map +1 -0
- package/dist/utils/ticketTierValidation.d.ts +8 -0
- package/dist/utils/ticketTierValidation.d.ts.map +1 -0
- package/dist/utils/ticketTierValidation.js +14 -0
- package/dist/utils/ticketTierValidation.js.map +1 -0
- package/package.json +1 -1
- package/prisma/schema.prisma +22 -0
- package/src/extendedSchemas.ts +25 -11
- package/src/index.ts +5 -0
- package/src/membershipDefinitions.ts +2 -2
- package/src/mirroredPrismaEnums.ts +1 -0
- package/src/utils/__tests__/eventTaskVolunteerRewardUtils.test.ts +57 -0
- package/src/utils/eventTaskVolunteerRewardUtils.ts +246 -0
- package/src/utils/organizationMembershipValidation.ts +18 -0
- package/src/utils/ticketTierValidation.ts +18 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EventTask,
|
|
3
|
+
EventTaskAssignment,
|
|
4
|
+
TaskStatus,
|
|
5
|
+
} from "@prisma/client";
|
|
6
|
+
|
|
7
|
+
/** Default volunteer slots when host does not set neededCount. */
|
|
8
|
+
export const DEFAULT_EVENT_TASK_NEEDED_COUNT = 1;
|
|
9
|
+
|
|
10
|
+
export const MIN_EVENT_TASK_NEEDED_COUNT = 1;
|
|
11
|
+
export const MAX_EVENT_TASK_NEEDED_COUNT = 99;
|
|
12
|
+
|
|
13
|
+
export type EventTaskAssignmentLike = Pick<
|
|
14
|
+
EventTaskAssignment,
|
|
15
|
+
"status" | "bashPointsPaidAt"
|
|
16
|
+
> & { userId: string };
|
|
17
|
+
|
|
18
|
+
export type EventTaskSlotsLike = Pick<
|
|
19
|
+
EventTask,
|
|
20
|
+
"neededCount" | "bashPointsReward" | "assignedToId"
|
|
21
|
+
> & {
|
|
22
|
+
status?: EventTask["status"];
|
|
23
|
+
assignments?: EventTaskAssignmentLike[] | null;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const ACTIVE_ASSIGNMENT_STATUSES = new Set<TaskStatus>([
|
|
27
|
+
"Pending",
|
|
28
|
+
"Assigned",
|
|
29
|
+
"Accepted",
|
|
30
|
+
"InProgress",
|
|
31
|
+
"Completed",
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
export function eventTaskNeededCount(
|
|
35
|
+
task: Pick<EventTask, "neededCount">
|
|
36
|
+
): number {
|
|
37
|
+
const n = task.neededCount ?? DEFAULT_EVENT_TASK_NEEDED_COUNT;
|
|
38
|
+
if (!Number.isFinite(n) || n < MIN_EVENT_TASK_NEEDED_COUNT) {
|
|
39
|
+
return DEFAULT_EVENT_TASK_NEEDED_COUNT;
|
|
40
|
+
}
|
|
41
|
+
return Math.min(Math.floor(n), MAX_EVENT_TASK_NEEDED_COUNT);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Counts filled volunteer slots (legacy assignedToId + assignment rows). */
|
|
45
|
+
export function countEventTaskFilledSlots(task: EventTaskSlotsLike): number {
|
|
46
|
+
const assignments = task.assignments ?? [];
|
|
47
|
+
const fromRows = assignments.filter(
|
|
48
|
+
(a) => a.status && ACTIVE_ASSIGNMENT_STATUSES.has(a.status)
|
|
49
|
+
).length;
|
|
50
|
+
|
|
51
|
+
if (task.assignedToId) {
|
|
52
|
+
const legacyCounted = assignments.some(
|
|
53
|
+
(a) => a.userId === task.assignedToId
|
|
54
|
+
);
|
|
55
|
+
if (!legacyCounted) {
|
|
56
|
+
return fromRows + 1;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return fromRows;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function countEventTaskOpenSlots(task: EventTaskSlotsLike): number {
|
|
63
|
+
return Math.max(
|
|
64
|
+
0,
|
|
65
|
+
eventTaskNeededCount(task) - countEventTaskFilledSlots(task)
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function eventTaskHasOpenSlots(task: EventTaskSlotsLike): boolean {
|
|
70
|
+
return countEventTaskOpenSlots(task) > 0;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Per-volunteer reward (0 when unset). */
|
|
74
|
+
export function eventTaskBashPointsPerVolunteer(
|
|
75
|
+
task: Pick<EventTask, "bashPointsReward">
|
|
76
|
+
): number {
|
|
77
|
+
const n = task.bashPointsReward ?? 0;
|
|
78
|
+
if (!Number.isFinite(n) || n <= 0) return 0;
|
|
79
|
+
return Math.floor(n);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Max BashPoints liability if every slot completes. */
|
|
83
|
+
export function eventTaskMaxBashPointsLiability(
|
|
84
|
+
task: Pick<EventTask, "neededCount" | "bashPointsReward">
|
|
85
|
+
): number {
|
|
86
|
+
return eventTaskNeededCount(task) * eventTaskBashPointsPerVolunteer(task);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type EventTaskOfferRow = Pick<
|
|
90
|
+
EventTask,
|
|
91
|
+
"id" | "neededCount" | "bashPointsReward"
|
|
92
|
+
>;
|
|
93
|
+
|
|
94
|
+
export function totalEventTaskBashPointsOffer(args: {
|
|
95
|
+
tasks: EventTaskOfferRow[];
|
|
96
|
+
patch?: {
|
|
97
|
+
taskId: string;
|
|
98
|
+
neededCount?: number | null;
|
|
99
|
+
bashPointsReward?: number | null;
|
|
100
|
+
};
|
|
101
|
+
}): number {
|
|
102
|
+
let total = 0;
|
|
103
|
+
for (const t of args.tasks) {
|
|
104
|
+
const isPatch = args.patch?.taskId === t.id;
|
|
105
|
+
const neededCount = isPatch
|
|
106
|
+
? (args.patch?.neededCount ?? t.neededCount)
|
|
107
|
+
: t.neededCount;
|
|
108
|
+
const bashPointsReward = isPatch
|
|
109
|
+
? (args.patch?.bashPointsReward ?? t.bashPointsReward)
|
|
110
|
+
: t.bashPointsReward;
|
|
111
|
+
total += eventTaskMaxBashPointsLiability({
|
|
112
|
+
neededCount,
|
|
113
|
+
bashPointsReward,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return total;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export type HostTaskOfferValidation =
|
|
120
|
+
{ ok: true } | { ok: false; reason: string };
|
|
121
|
+
|
|
122
|
+
export function validateHostTaskBashPointsOffer(args: {
|
|
123
|
+
hostBashPointsBalance: number;
|
|
124
|
+
tasks: EventTaskOfferRow[];
|
|
125
|
+
patch?: {
|
|
126
|
+
taskId: string;
|
|
127
|
+
neededCount?: number | null;
|
|
128
|
+
bashPointsReward?: number | null;
|
|
129
|
+
};
|
|
130
|
+
}): HostTaskOfferValidation {
|
|
131
|
+
const perVolunteer = args.patch
|
|
132
|
+
? eventTaskBashPointsPerVolunteer({
|
|
133
|
+
bashPointsReward: args.patch.bashPointsReward ?? null,
|
|
134
|
+
})
|
|
135
|
+
: 0;
|
|
136
|
+
const needed = args.patch
|
|
137
|
+
? eventTaskNeededCount({ neededCount: args.patch.neededCount ?? null })
|
|
138
|
+
: 0;
|
|
139
|
+
|
|
140
|
+
if (args.patch && perVolunteer > 0 && needed < MIN_EVENT_TASK_NEEDED_COUNT) {
|
|
141
|
+
return {
|
|
142
|
+
ok: false,
|
|
143
|
+
reason: "Set at least one volunteer slot when offering BashPoints.",
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (perVolunteer < 0) {
|
|
148
|
+
return { ok: false, reason: "BashPoints reward cannot be negative." };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const totalOffer = totalEventTaskBashPointsOffer({
|
|
152
|
+
tasks: args.tasks,
|
|
153
|
+
patch: args.patch,
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
if (totalOffer > args.hostBashPointsBalance) {
|
|
157
|
+
return {
|
|
158
|
+
ok: false,
|
|
159
|
+
reason: `Total BashPoints offered across tasks (${totalOffer}) exceeds your balance (${args.hostBashPointsBalance}). Lower rewards or slots, or earn more BashPoints.`,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return { ok: true };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function formatEventTaskBashPointsBadge(
|
|
167
|
+
task: Pick<EventTask, "bashPointsReward">
|
|
168
|
+
): string | null {
|
|
169
|
+
const bp = eventTaskBashPointsPerVolunteer(task);
|
|
170
|
+
if (bp <= 0) return null;
|
|
171
|
+
return `+${bp} BP`;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function formatEventTaskAssignmentSlotsLabel(
|
|
175
|
+
task: EventTaskSlotsLike
|
|
176
|
+
): string {
|
|
177
|
+
const needed = eventTaskNeededCount(task);
|
|
178
|
+
const filled = countEventTaskFilledSlots(task);
|
|
179
|
+
return `${filled}/${needed} assigned`;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function isUserAssignedToEventTask(
|
|
183
|
+
task: EventTaskSlotsLike & { assignedToId?: string | null },
|
|
184
|
+
userId: string | null | undefined
|
|
185
|
+
): boolean {
|
|
186
|
+
if (!userId) return false;
|
|
187
|
+
if (task.assignedToId === userId) return true;
|
|
188
|
+
return (task.assignments ?? []).some(
|
|
189
|
+
(a) =>
|
|
190
|
+
a.userId === userId &&
|
|
191
|
+
a.status &&
|
|
192
|
+
ACTIVE_ASSIGNMENT_STATUSES.has(a.status)
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Validate edits before coercion so invalid values never silently change. */
|
|
197
|
+
export function validateEventTaskFields(
|
|
198
|
+
task: {
|
|
199
|
+
title?: unknown;
|
|
200
|
+
neededCount?: unknown;
|
|
201
|
+
bashPointsReward?: unknown;
|
|
202
|
+
doBy?: unknown;
|
|
203
|
+
dueBy?: unknown;
|
|
204
|
+
},
|
|
205
|
+
requireTitle = false
|
|
206
|
+
): string | null {
|
|
207
|
+
if (
|
|
208
|
+
(requireTitle || task.title !== undefined) &&
|
|
209
|
+
(typeof task.title !== "string" || !task.title.trim())
|
|
210
|
+
)
|
|
211
|
+
return "Enter a task title";
|
|
212
|
+
if (typeof task.title === "string" && task.title.trim().length > 150)
|
|
213
|
+
return "Task titles must be 150 characters or fewer";
|
|
214
|
+
for (const [key, min, max, label] of [
|
|
215
|
+
["neededCount", 1, 99, "Volunteer slots"],
|
|
216
|
+
["bashPointsReward", 0, 2147483647, "BashPoints"],
|
|
217
|
+
] as const) {
|
|
218
|
+
const value = task[key];
|
|
219
|
+
if (value == null || value === "") continue;
|
|
220
|
+
const n = Number(value);
|
|
221
|
+
if (!Number.isInteger(n) || n < min || n > max)
|
|
222
|
+
return `${label} must be a whole number from ${min} to ${max}`;
|
|
223
|
+
}
|
|
224
|
+
for (const value of [task.doBy, task.dueBy])
|
|
225
|
+
if (
|
|
226
|
+
value != null &&
|
|
227
|
+
value !== "" &&
|
|
228
|
+
Number.isNaN(new Date(value as string).getTime())
|
|
229
|
+
)
|
|
230
|
+
return "Enter a valid task date";
|
|
231
|
+
if (
|
|
232
|
+
task.doBy &&
|
|
233
|
+
task.dueBy &&
|
|
234
|
+
new Date(task.dueBy as string) < new Date(task.doBy as string)
|
|
235
|
+
)
|
|
236
|
+
return "Due by must be on or after Do by";
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Acceptance is per volunteer; another volunteer's acceptance grants no access. */
|
|
241
|
+
export function isUserAcceptedForEventTask(task: EventTaskSlotsLike, userId: string | null | undefined): boolean {
|
|
242
|
+
if (!userId) return false;
|
|
243
|
+
const accepted = new Set<TaskStatus>(["Accepted", "Assigned", "InProgress", "Completed"]);
|
|
244
|
+
const own = task.assignments?.find(assignment => assignment.userId === userId);
|
|
245
|
+
return own ? accepted.has(own.status) : task.assignedToId === userId && Boolean(task.status && accepted.has(task.status));
|
|
246
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Membership prices are stored as whole cents in a signed 32-bit column. */
|
|
2
|
+
export function membershipDollarsToCents(value: string): number | null {
|
|
3
|
+
if (!/^\d+(?:\.\d{1,2})?$/.test(value.trim())) return null;
|
|
4
|
+
const cents = Math.round(Number(value) * 100);
|
|
5
|
+
return Number.isSafeInteger(cents) && cents <= 2147483647 ? cents : null;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function validateOrganizationMembershipPrices(values: {
|
|
9
|
+
baseMembershipPriceCents?: unknown;
|
|
10
|
+
premiumMembershipPriceCents?: unknown;
|
|
11
|
+
}): string | null {
|
|
12
|
+
for (const value of [values.baseMembershipPriceCents, values.premiumMembershipPriceCents]) {
|
|
13
|
+
if (value != null && (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0 || value > 2147483647)) {
|
|
14
|
+
return "Membership fees must be non-negative whole cents within the supported range.";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Monetary amounts are cents; null capacity means unlimited, never zero. */
|
|
2
|
+
export function validateTicketTierFields(tier: {
|
|
3
|
+
title?: string | null;
|
|
4
|
+
price?: number | null;
|
|
5
|
+
priceBashPoints?: number | null;
|
|
6
|
+
maximumNumberOfTickets?: number | null;
|
|
7
|
+
}, requireTitle = true): string | null {
|
|
8
|
+
if ((requireTitle || tier.title !== undefined) && !tier.title?.trim())
|
|
9
|
+
return "Give every ticket tier a name before saving.";
|
|
10
|
+
for (const [label, value] of [["Ticket price", tier.price], ["BashPoints price", tier.priceBashPoints]] as const) {
|
|
11
|
+
if (value != null && (!Number.isSafeInteger(value) || value < 0 || value > 2147483647))
|
|
12
|
+
return `${label} must be a non-negative whole amount${label === "Ticket price" ? " in cents" : ""}.`;
|
|
13
|
+
}
|
|
14
|
+
const count = tier.maximumNumberOfTickets;
|
|
15
|
+
if (count != null && (!Number.isInteger(count) || count < 1 || count > 2147483647))
|
|
16
|
+
return "Ticket quantity must be a positive whole number. Leave it blank for unlimited tickets.";
|
|
17
|
+
return null;
|
|
18
|
+
}
|