@frigear-nu/crew 0.0.0-stage → 0.0.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/README.md +39 -2
- package/dist/module.d.mts +244 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +155 -0
- package/dist/runtime/access.d.ts +1 -0
- package/dist/runtime/access.js +45 -0
- package/dist/runtime/app/components/CrewAddMemberSlideover.d.vue.ts +18 -0
- package/dist/runtime/app/components/CrewAddMemberSlideover.vue +198 -0
- package/dist/runtime/app/components/CrewAddMemberSlideover.vue.d.ts +18 -0
- package/dist/runtime/app/components/CrewInviteSlideover.d.vue.ts +14 -0
- package/dist/runtime/app/components/CrewInviteSlideover.vue +147 -0
- package/dist/runtime/app/components/CrewInviteSlideover.vue.d.ts +14 -0
- package/dist/runtime/app/components/CrewMemberSlideover.d.vue.ts +14 -0
- package/dist/runtime/app/components/CrewMemberSlideover.vue +126 -0
- package/dist/runtime/app/components/CrewMemberSlideover.vue.d.ts +14 -0
- package/dist/runtime/app/composables/index.d.ts +4 -0
- package/dist/runtime/app/composables/index.js +3 -0
- package/dist/runtime/app/composables/useCrewActions.d.ts +45 -0
- package/dist/runtime/app/composables/useCrewActions.js +122 -0
- package/dist/runtime/app/composables/useCrewLinks.d.ts +9 -0
- package/dist/runtime/app/composables/useCrewLinks.js +6 -0
- package/dist/runtime/app/composables/useCrewTranslate.d.ts +7 -0
- package/dist/runtime/app/composables/useCrewTranslate.js +15 -0
- package/dist/runtime/app/dashboard/CrewStatus.d.vue.ts +3 -0
- package/dist/runtime/app/dashboard/CrewStatus.vue +78 -0
- package/dist/runtime/app/dashboard/CrewStatus.vue.d.ts +3 -0
- package/dist/runtime/app/pages/apply.d.vue.ts +3 -0
- package/dist/runtime/app/pages/apply.vue +95 -0
- package/dist/runtime/app/pages/apply.vue.d.ts +3 -0
- package/dist/runtime/app/pages/home.d.vue.ts +3 -0
- package/dist/runtime/app/pages/home.vue +21 -0
- package/dist/runtime/app/pages/home.vue.d.ts +3 -0
- package/dist/runtime/app/pages/invite.d.vue.ts +3 -0
- package/dist/runtime/app/pages/invite.vue +86 -0
- package/dist/runtime/app/pages/invite.vue.d.ts +3 -0
- package/dist/runtime/app/pages/status.d.vue.ts +3 -0
- package/dist/runtime/app/pages/status.vue +82 -0
- package/dist/runtime/app/pages/status.vue.d.ts +3 -0
- package/dist/runtime/app/tabs/Crew.d.vue.ts +9 -0
- package/dist/runtime/app/tabs/Crew.vue +590 -0
- package/dist/runtime/app/tabs/Crew.vue.d.ts +9 -0
- package/dist/runtime/app/utils/admin-crew.d.ts +48 -0
- package/dist/runtime/app/utils/admin-crew.js +23 -0
- package/dist/runtime/app/widgets/InboxAddToCrew.d.vue.ts +13 -0
- package/dist/runtime/app/widgets/InboxAddToCrew.vue +34 -0
- package/dist/runtime/app/widgets/InboxAddToCrew.vue.d.ts +13 -0
- package/dist/runtime/dashboard.d.ts +8 -0
- package/dist/runtime/dashboard.js +14 -0
- package/dist/runtime/emails/crew-status-applied.d.vue.ts +16 -0
- package/dist/runtime/emails/crew-status-applied.vue +40 -0
- package/dist/runtime/emails/crew-status-applied.vue.d.ts +16 -0
- package/dist/runtime/emails/crew-status-approved.d.vue.ts +16 -0
- package/dist/runtime/emails/crew-status-approved.vue +40 -0
- package/dist/runtime/emails/crew-status-approved.vue.d.ts +16 -0
- package/dist/runtime/emails/crew-status-rejected.d.vue.ts +15 -0
- package/dist/runtime/emails/crew-status-rejected.vue +33 -0
- package/dist/runtime/emails/crew-status-rejected.vue.d.ts +15 -0
- package/dist/runtime/emails/crew-status-waitlisted.d.vue.ts +16 -0
- package/dist/runtime/emails/crew-status-waitlisted.vue +40 -0
- package/dist/runtime/emails/crew-status-waitlisted.vue.d.ts +16 -0
- package/dist/runtime/emails/crew-status-withdrawn.d.vue.ts +16 -0
- package/dist/runtime/emails/crew-status-withdrawn.vue +40 -0
- package/dist/runtime/emails/crew-status-withdrawn.vue.d.ts +16 -0
- package/dist/runtime/i18n/lang/da.json +237 -0
- package/dist/runtime/i18n/lang/en.json +237 -0
- package/dist/runtime/menus.d.ts +2 -0
- package/dist/runtime/menus.js +17 -0
- package/dist/runtime/notifications.d.ts +5 -0
- package/dist/runtime/notifications.js +10 -0
- package/dist/runtime/server/db/schema.postgresql.d.ts +433 -0
- package/dist/runtime/server/db/schema.postgresql.js +43 -0
- package/dist/runtime/server/db/schema.sqlite.d.ts +451 -0
- package/dist/runtime/server/db/schema.sqlite.js +43 -0
- package/dist/runtime/server/db/team-members.postgresql.d.ts +143 -0
- package/dist/runtime/server/db/team-members.postgresql.js +20 -0
- package/dist/runtime/server/db/team-members.sqlite.d.ts +151 -0
- package/dist/runtime/server/db/team-members.sqlite.js +20 -0
- package/dist/runtime/server/plugins/forms.d.ts +2 -0
- package/dist/runtime/server/plugins/forms.js +27 -0
- package/dist/runtime/server/plugins/participation.d.ts +2 -0
- package/dist/runtime/server/plugins/participation.js +16 -0
- package/dist/runtime/server/policies.d.ts +2 -0
- package/dist/runtime/server/policies.js +10 -0
- package/dist/runtime/server/routes/admin/forms.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/forms.get.js +8 -0
- package/dist/runtime/server/routes/admin/invite.delete.d.ts +2 -0
- package/dist/runtime/server/routes/admin/invite.delete.js +13 -0
- package/dist/runtime/server/routes/admin/invites.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/invites.get.js +14 -0
- package/dist/runtime/server/routes/admin/invites.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/invites.post.js +31 -0
- package/dist/runtime/server/routes/admin/member-approve.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/member-approve.post.js +10 -0
- package/dist/runtime/server/routes/admin/member-reject.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/member-reject.post.js +9 -0
- package/dist/runtime/server/routes/admin/member-submission.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/member-submission.get.js +17 -0
- package/dist/runtime/server/routes/admin/member-waitlist.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/member-waitlist.post.js +9 -0
- package/dist/runtime/server/routes/admin/member-withdraw.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/member-withdraw.post.js +9 -0
- package/dist/runtime/server/routes/admin/member.patch.d.ts +2 -0
- package/dist/runtime/server/routes/admin/member.patch.js +10 -0
- package/dist/runtime/server/routes/admin/members-approve.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/members-approve.post.js +12 -0
- package/dist/runtime/server/routes/admin/members.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/members.get.js +10 -0
- package/dist/runtime/server/routes/admin/members.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/members.post.js +30 -0
- package/dist/runtime/server/routes/admin/settings.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/settings.get.js +9 -0
- package/dist/runtime/server/routes/admin/settings.put.d.ts +2 -0
- package/dist/runtime/server/routes/admin/settings.put.js +12 -0
- package/dist/runtime/server/routes/admin/users.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/users.get.js +8 -0
- package/dist/runtime/server/routes/me/crew.get.d.ts +2 -0
- package/dist/runtime/server/routes/me/crew.get.js +5 -0
- package/dist/runtime/server/routes/public/apply.get.d.ts +2 -0
- package/dist/runtime/server/routes/public/apply.get.js +9 -0
- package/dist/runtime/server/routes/public/invite-accept.post.d.ts +2 -0
- package/dist/runtime/server/routes/public/invite-accept.post.js +7 -0
- package/dist/runtime/server/routes/public/member.get.d.ts +2 -0
- package/dist/runtime/server/routes/public/member.get.js +9 -0
- package/dist/runtime/server/routes/public/members.get.d.ts +2 -0
- package/dist/runtime/server/routes/public/members.get.js +11 -0
- package/dist/runtime/server/utils/crew-dto.d.ts +46 -0
- package/dist/runtime/server/utils/crew-dto.js +80 -0
- package/dist/runtime/server/utils/crew-emails.d.ts +43 -0
- package/dist/runtime/server/utils/crew-emails.js +139 -0
- package/dist/runtime/server/utils/crew-schemas.d.ts +121 -0
- package/dist/runtime/server/utils/crew-schemas.js +69 -0
- package/dist/runtime/server/utils/crew-send.d.ts +19 -0
- package/dist/runtime/server/utils/crew-send.js +20 -0
- package/dist/runtime/server/utils/crew-settings.d.ts +52 -0
- package/dist/runtime/server/utils/crew-settings.js +100 -0
- package/dist/runtime/server/utils/crew-tokens.d.ts +8 -0
- package/dist/runtime/server/utils/crew-tokens.js +9 -0
- package/dist/runtime/server/utils/crew.d.ts +172 -0
- package/dist/runtime/server/utils/crew.js +502 -0
- package/dist/runtime/utils/api.d.ts +44 -0
- package/dist/runtime/utils/api.js +47 -0
- package/dist/runtime/utils/index.d.ts +8 -0
- package/dist/runtime/utils/index.js +6 -0
- package/dist/runtime/utils/links.d.ts +25 -0
- package/dist/runtime/utils/links.js +14 -0
- package/dist/runtime/utils/status.d.ts +36 -0
- package/dist/runtime/utils/status.js +39 -0
- package/dist/types.d.mts +5 -0
- package/package.json +118 -3
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
import { db, schema } from "@nuxthub/db";
|
|
2
|
+
import { aliasedTable, and, asc, count, desc, eq, exists, gt, inArray, isNotNull, isNull, lt, max, ne, not, or, sql } from "drizzle-orm";
|
|
3
|
+
import { apiError, chunkParams, conflict, isUniqueViolation, likeContains, notFound, orderByQuery, pageRange, paginate, searchContains } from "@frigear-nu/module-kit/server";
|
|
4
|
+
import { ladderKeys } from "@frigear-nu/teams/utils";
|
|
5
|
+
import { crewAccess } from "../../access.js";
|
|
6
|
+
import { canMoveCrew, newId, teamStatusOf, translate } from "../../utils/index.js";
|
|
7
|
+
import { toCrewMemberDto, toMyCrewDto } from "./crew-dto.js";
|
|
8
|
+
import { crewSendStatusEmail } from "./crew-emails.js";
|
|
9
|
+
import { crewApprovedCount, crewDefaultTeamOf, crewLowestRole, crewOperationTeams, findCrewOperationTeam, findCrewSettings, getCrewOperationTeamOr400 } from "./crew-settings.js";
|
|
10
|
+
import { crewHashToken } from "./crew-tokens.js";
|
|
11
|
+
const operationTeamOwned = (operationId) => and(eq(schema.teams.ownerType, "operation"), eq(schema.teams.ownerId, operationId));
|
|
12
|
+
export async function crewUsersById(ids) {
|
|
13
|
+
return loadUserRefs(ids);
|
|
14
|
+
}
|
|
15
|
+
export async function findCrewEntry(userId, operationId) {
|
|
16
|
+
const [row] = await db.select({ member: schema.teamMembers, team: schema.teams }).from(schema.teamMembers).innerJoin(schema.teams, eq(schema.teamMembers.teamId, schema.teams.id)).where(and(eq(schema.teamMembers.userId, userId), operationTeamOwned(operationId), isNotNull(schema.teamMembers.crewStatus))).orderBy(sql`case when ${schema.teamMembers.crewStatus} = 'withdrawn' then 1 else 0 end`, asc(schema.teamMembers.createdAt), asc(schema.teamMembers.id)).limit(1);
|
|
17
|
+
return row;
|
|
18
|
+
}
|
|
19
|
+
export async function getCrewEntryOr404(id) {
|
|
20
|
+
const [row] = await db.select({ member: schema.teamMembers, team: schema.teams }).from(schema.teamMembers).innerJoin(schema.teams, eq(schema.teamMembers.teamId, schema.teams.id)).where(and(eq(schema.teamMembers.id, id), eq(schema.teams.ownerType, "operation"), isNotNull(schema.teamMembers.crewStatus))).limit(1);
|
|
21
|
+
if (!row) throw notFound("Crew member not found.");
|
|
22
|
+
return row;
|
|
23
|
+
}
|
|
24
|
+
export const crewTakesPart = () => and(
|
|
25
|
+
or(isNull(schema.teamMembers.status), eq(schema.teamMembers.status, "active")),
|
|
26
|
+
or(isNull(schema.teamMembers.crewStatus), eq(schema.teamMembers.crewStatus, "approved"))
|
|
27
|
+
);
|
|
28
|
+
export async function crewHasPlainMembership(userId, operationId) {
|
|
29
|
+
const [row] = await db.select({ id: schema.teamMembers.id }).from(schema.teamMembers).innerJoin(schema.teams, eq(schema.teamMembers.teamId, schema.teams.id)).where(and(eq(schema.teamMembers.userId, userId), operationTeamOwned(operationId), isNull(schema.teamMembers.crewStatus), crewTakesPart())).limit(1);
|
|
30
|
+
return !!row;
|
|
31
|
+
}
|
|
32
|
+
function crewPreviousExists(operation, user) {
|
|
33
|
+
const prev = aliasedTable(schema.teamMembers, "prev_member");
|
|
34
|
+
const prevTeam = aliasedTable(schema.teams, "prev_team");
|
|
35
|
+
return exists(db.select({ one: sql`1` }).from(prev).innerJoin(prevTeam, eq(prev.teamId, prevTeam.id)).innerJoin(schema.operations, eq(schema.operations.id, prevTeam.ownerId)).where(and(
|
|
36
|
+
eq(prev.userId, user),
|
|
37
|
+
eq(prev.crewStatus, "approved"),
|
|
38
|
+
eq(prevTeam.ownerType, "operation"),
|
|
39
|
+
ne(schema.operations.id, operation.id),
|
|
40
|
+
lt(schema.operations.startsAt, operation.startsAt)
|
|
41
|
+
)));
|
|
42
|
+
}
|
|
43
|
+
export async function crewIsPreviousCrew(operation, userId) {
|
|
44
|
+
const [row] = await db.select({ found: sql`case when ${crewPreviousExists(operation, userId)} then 1 else 0 end` }).from(schema.operations).where(eq(schema.operations.id, operation.id)).limit(1);
|
|
45
|
+
return Number(row?.found ?? 0) === 1;
|
|
46
|
+
}
|
|
47
|
+
const teamLite = (team) => ({ id: team.id, name: team.name, ownerId: team.ownerId ?? null });
|
|
48
|
+
export async function crewMemberDtoOf(entry, operation, pii) {
|
|
49
|
+
const [users, previous] = await Promise.all([crewUsersById([entry.member.userId]), crewIsPreviousCrew(operation, entry.member.userId)]);
|
|
50
|
+
return toCrewMemberDto({ member: entry.member, team: teamLite(entry.team), user: users.get(entry.member.userId), previous, pii });
|
|
51
|
+
}
|
|
52
|
+
export async function crewMemberResponse(event, entry) {
|
|
53
|
+
const operation = await getOperationOr404(entry.team.ownerId);
|
|
54
|
+
return crewMemberDtoOf(entry, operation, await allows(event, crewAccess.permissions.pii));
|
|
55
|
+
}
|
|
56
|
+
export async function crewRoleLabelOf(event, kind, role) {
|
|
57
|
+
return (await loadTeamLadders(event))[kind]?.roles.find((item) => item.key === role)?.label ?? role;
|
|
58
|
+
}
|
|
59
|
+
export async function crewRoleLabel(event, kind, role, locale = "en") {
|
|
60
|
+
const ladder = (await loadTeamLadders(event))[kind];
|
|
61
|
+
return translate(ladder?.roles.find((item) => item.key === role)?.label, locale, "en") || role;
|
|
62
|
+
}
|
|
63
|
+
export async function crewMyEntryOf(event, entry, operation) {
|
|
64
|
+
return toMyCrewDto({ member: entry.member, team: teamLite(entry.team), operation, roleLabel: await crewRoleLabelOf(event, entry.team.kind, entry.member.role) });
|
|
65
|
+
}
|
|
66
|
+
export async function crewMyEntries(event, userId) {
|
|
67
|
+
const rows = await db.select({ member: schema.teamMembers, team: schema.teams, operation: schema.operations }).from(schema.teamMembers).innerJoin(schema.teams, eq(schema.teamMembers.teamId, schema.teams.id)).innerJoin(schema.operations, eq(schema.operations.id, schema.teams.ownerId)).where(and(eq(schema.teamMembers.userId, userId), eq(schema.teams.ownerType, "operation"), or(isNotNull(schema.teamMembers.crewStatus), crewTakesPart()))).orderBy(desc(schema.operations.startsAt));
|
|
68
|
+
return Promise.all(rows.map(async (row) => toMyCrewDto({ member: row.member, team: teamLite(row.team), operation: row.operation, roleLabel: await crewRoleLabelOf(event, row.team.kind, row.member.role) })));
|
|
69
|
+
}
|
|
70
|
+
const crewParticipating = () => and(eq(schema.teams.ownerType, "operation"), crewTakesPart());
|
|
71
|
+
export async function crewParticipationOperationIds(userId) {
|
|
72
|
+
const rows = await db.selectDistinct({ operationId: schema.teams.ownerId }).from(schema.teamMembers).innerJoin(schema.teams, eq(schema.teamMembers.teamId, schema.teams.id)).where(and(eq(schema.teamMembers.userId, userId), crewParticipating()));
|
|
73
|
+
return rows.flatMap((row) => row.operationId ? [row.operationId] : []);
|
|
74
|
+
}
|
|
75
|
+
export async function crewOperationParticipants(operationIds) {
|
|
76
|
+
const ids = /* @__PURE__ */ new Set();
|
|
77
|
+
for (const chunk of chunkParams([...new Set(operationIds)])) {
|
|
78
|
+
const rows = await db.selectDistinct({ userId: schema.teamMembers.userId }).from(schema.teamMembers).innerJoin(schema.teams, eq(schema.teamMembers.teamId, schema.teams.id)).where(and(inArray(schema.teams.ownerId, chunk), crewParticipating()));
|
|
79
|
+
for (const row of rows) ids.add(String(row.userId));
|
|
80
|
+
}
|
|
81
|
+
return [...ids];
|
|
82
|
+
}
|
|
83
|
+
export async function crewUserReadsOperation(event, operation, userId) {
|
|
84
|
+
if (operation.public) return true;
|
|
85
|
+
return operationReadable(event, operation, { id: userId }, await userCan(event, userId, "operations:manage"));
|
|
86
|
+
}
|
|
87
|
+
export function crewOpenState(operation, settings, now = /* @__PURE__ */ new Date()) {
|
|
88
|
+
if (!settings?.formId) return "unavailable";
|
|
89
|
+
if (operation.status === "archived" || operation.status === "wrapped_up") return "closed";
|
|
90
|
+
if (settings.opensAt && now.getTime() < settings.opensAt.getTime()) return "scheduled";
|
|
91
|
+
if (settings.closesAt && now.getTime() >= settings.closesAt.getTime()) return "closed";
|
|
92
|
+
return "open";
|
|
93
|
+
}
|
|
94
|
+
export async function crewApplyDto(operation, settings, userId) {
|
|
95
|
+
const entry = await findCrewEntry(userId, operation.id);
|
|
96
|
+
const status = entry?.member.crewStatus ?? null;
|
|
97
|
+
let state;
|
|
98
|
+
if (status === "applied" || status === "waitlisted" || status === "approved") state = "applied";
|
|
99
|
+
else if (status === "rejected") state = "rejected";
|
|
100
|
+
else if (await crewHasPlainMembership(userId, operation.id)) state = "member";
|
|
101
|
+
let formSlug = null;
|
|
102
|
+
if (!state) {
|
|
103
|
+
state = crewOpenState(operation, settings);
|
|
104
|
+
if (state === "open") {
|
|
105
|
+
const [form] = await db.select({ slug: schema.forms.slug, status: schema.forms.status }).from(schema.forms).where(eq(schema.forms.id, settings.formId)).limit(1);
|
|
106
|
+
if (form?.status === "published") formSlug = form.slug;
|
|
107
|
+
else state = "unavailable";
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
operationId: operation.id,
|
|
112
|
+
operationSlug: operation.slug,
|
|
113
|
+
operationName: operation.name,
|
|
114
|
+
state,
|
|
115
|
+
crewStatus: status,
|
|
116
|
+
formSlug,
|
|
117
|
+
opensAt: settings?.opensAt?.toISOString() ?? null,
|
|
118
|
+
closesAt: settings?.closesAt?.toISOString() ?? null
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
export async function crewStorePhone(userId, value) {
|
|
122
|
+
const phone = parseUserPhone(value);
|
|
123
|
+
if (!phone) return;
|
|
124
|
+
await db.update(schema.users).set({ phone }).where(eq(schema.users.id, userId));
|
|
125
|
+
}
|
|
126
|
+
async function crewFire(event, name, operationId, member) {
|
|
127
|
+
await useNitroApp().hooks.callHook(name, { event, operationId, member });
|
|
128
|
+
}
|
|
129
|
+
export function crewRequestLocale(event) {
|
|
130
|
+
const tag = getRequestHeader(event, "accept-language")?.split(",")[0]?.trim().split(/[-_;]/)[0]?.toLowerCase();
|
|
131
|
+
return tag && /^[a-z]{2,3}$/.test(tag) ? tag : void 0;
|
|
132
|
+
}
|
|
133
|
+
const primaryLocale = (locale) => locale?.split(/[-_]/)[0]?.toLowerCase() || void 0;
|
|
134
|
+
async function crewFireTeamsHooks(event, before, after) {
|
|
135
|
+
const hadGrant = !!before && teamMemberStatusGrants(before.member.status);
|
|
136
|
+
const hasGrant = teamMemberStatusGrants(after.member.status);
|
|
137
|
+
const hooks = useNitroApp().hooks;
|
|
138
|
+
const ctxOf = async (entry) => ({ event, team: await toAdminTeamDtoOf(event, entry.team), member: await teamMemberDtoOf(entry.member, true) });
|
|
139
|
+
if (!hadGrant && hasGrant) return hooks.callHook("teams:member:added", await ctxOf(after));
|
|
140
|
+
if (hadGrant && !hasGrant) return hooks.callHook("teams:member:removed", await ctxOf(before));
|
|
141
|
+
if (!hadGrant || !hasGrant) return;
|
|
142
|
+
if (before.team.id !== after.team.id) {
|
|
143
|
+
await hooks.callHook("teams:member:removed", await ctxOf(before));
|
|
144
|
+
return hooks.callHook("teams:member:added", await ctxOf(after));
|
|
145
|
+
}
|
|
146
|
+
if (before.member.role !== after.member.role) {
|
|
147
|
+
await hooks.callHook("teams:member:role-changed", { ...await ctxOf(after), from: before.member.role, to: after.member.role });
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
async function crewInsertMember(event, input) {
|
|
151
|
+
const { team, userId, role, addedBy, fire = true, ...columns } = input;
|
|
152
|
+
const taken = () => conflict("This user is a member of the team already.", { field: "userId" });
|
|
153
|
+
if (await findTeamMember(team.id, userId)) throw taken();
|
|
154
|
+
const [member] = await db.insert(schema.teamMembers).values({ id: newId("tmm"), teamId: team.id, userId, role, status: teamStatusOf(input.crewStatus), addedBy, ...columns }).returning().catch((error) => {
|
|
155
|
+
if (isUniqueViolation(error)) throw taken();
|
|
156
|
+
throw error;
|
|
157
|
+
});
|
|
158
|
+
forgetTeamRoles(event);
|
|
159
|
+
const entry = { member, team };
|
|
160
|
+
if (fire) await crewFireTeamsHooks(event, void 0, entry);
|
|
161
|
+
return entry;
|
|
162
|
+
}
|
|
163
|
+
async function crewWrite(event, before, patch) {
|
|
164
|
+
const values = { ...patch };
|
|
165
|
+
if (patch.crewStatus) values.status = teamStatusOf(patch.crewStatus);
|
|
166
|
+
const current = before.member.crewStatus;
|
|
167
|
+
const [member] = await db.update(schema.teamMembers).set(values).where(and(
|
|
168
|
+
eq(schema.teamMembers.id, before.member.id),
|
|
169
|
+
current === null ? isNull(schema.teamMembers.crewStatus) : eq(schema.teamMembers.crewStatus, current)
|
|
170
|
+
)).returning().catch((error) => {
|
|
171
|
+
if (patch.teamId && isUniqueViolation(error)) throw conflict("This user is a member of the team already.", { field: "teamId" });
|
|
172
|
+
throw error;
|
|
173
|
+
});
|
|
174
|
+
if (!member) return void 0;
|
|
175
|
+
forgetTeamRoles(event);
|
|
176
|
+
const team = patch.teamId && patch.teamId !== before.team.id ? await findTeamById(patch.teamId) ?? before.team : before.team;
|
|
177
|
+
return { member, team };
|
|
178
|
+
}
|
|
179
|
+
async function crewUpdateMember(event, before, patch) {
|
|
180
|
+
const written = await crewWrite(event, before, patch);
|
|
181
|
+
if (!written) return { entry: await getCrewEntryOr404(before.member.id).catch(() => before), changed: false };
|
|
182
|
+
await crewFireTeamsHooks(event, before, written);
|
|
183
|
+
return { entry: written, changed: true };
|
|
184
|
+
}
|
|
185
|
+
async function crewNextWaitlistPosition(operationId) {
|
|
186
|
+
const [row] = await db.select({ top: max(schema.teamMembers.waitlistPosition) }).from(schema.teamMembers).innerJoin(schema.teams, eq(schema.teamMembers.teamId, schema.teams.id)).where(and(operationTeamOwned(operationId), eq(schema.teamMembers.crewStatus, "waitlisted")));
|
|
187
|
+
return Number(row?.top ?? 0) + 1;
|
|
188
|
+
}
|
|
189
|
+
async function crewNotify(event, entry, operation, settings, status) {
|
|
190
|
+
const member = await crewMemberDtoOf(entry, operation, true);
|
|
191
|
+
const locale = (await loadUserRefs([entry.member.userId])).get(entry.member.userId)?.locale || entry.member.locale || void 0;
|
|
192
|
+
await crewSendStatusEmail(event, {
|
|
193
|
+
userId: entry.member.userId,
|
|
194
|
+
status,
|
|
195
|
+
member,
|
|
196
|
+
operationName: operation.name,
|
|
197
|
+
roleLabel: await crewRoleLabel(event, entry.team.kind, entry.member.role, locale),
|
|
198
|
+
settings,
|
|
199
|
+
locale
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
async function crewApplyStatus(operationId, settings) {
|
|
203
|
+
if (!settings || settings.capacity === null || !settings.waitlist) return "applied";
|
|
204
|
+
return await crewApprovedCount(operationId) >= settings.capacity ? "waitlisted" : "applied";
|
|
205
|
+
}
|
|
206
|
+
export async function crewApply(event, input) {
|
|
207
|
+
const { operation, settings, userId, submissionId } = input;
|
|
208
|
+
const locale = primaryLocale(input.locale) ?? null;
|
|
209
|
+
const now = /* @__PURE__ */ new Date();
|
|
210
|
+
const existing = await findCrewEntry(userId, operation.id);
|
|
211
|
+
let entry;
|
|
212
|
+
if (existing) {
|
|
213
|
+
if (existing.member.crewStatus !== "withdrawn") {
|
|
214
|
+
if (submissionId && existing.member.crewStatus !== "rejected" && submissionId !== existing.member.submissionId) {
|
|
215
|
+
await db.update(schema.teamMembers).set({ submissionId }).where(eq(schema.teamMembers.id, existing.member.id));
|
|
216
|
+
}
|
|
217
|
+
return { entry: existing, created: false };
|
|
218
|
+
}
|
|
219
|
+
const status2 = await crewApplyStatus(operation.id, settings);
|
|
220
|
+
const reopened = await crewUpdateMember(event, existing, {
|
|
221
|
+
crewStatus: status2,
|
|
222
|
+
appliedAt: now,
|
|
223
|
+
decidedAt: null,
|
|
224
|
+
decidedBy: null,
|
|
225
|
+
submissionId,
|
|
226
|
+
locale,
|
|
227
|
+
waitlistPosition: status2 === "waitlisted" ? await crewNextWaitlistPosition(operation.id) : null,
|
|
228
|
+
waitlistReason: status2 === "waitlisted" ? "capacity" : null
|
|
229
|
+
});
|
|
230
|
+
if (!reopened.changed) return { entry: reopened.entry, created: false };
|
|
231
|
+
entry = reopened.entry;
|
|
232
|
+
} else {
|
|
233
|
+
if (await crewHasPlainMembership(userId, operation.id)) return null;
|
|
234
|
+
const team = await crewDefaultTeamOf(event, operation.id, settings);
|
|
235
|
+
const status2 = await crewApplyStatus(operation.id, settings);
|
|
236
|
+
entry = await crewInsertMember(event, {
|
|
237
|
+
team,
|
|
238
|
+
userId,
|
|
239
|
+
role: await crewLowestRole(event, team.kind),
|
|
240
|
+
crewStatus: status2,
|
|
241
|
+
appliedAt: now,
|
|
242
|
+
submissionId,
|
|
243
|
+
locale,
|
|
244
|
+
waitlistPosition: status2 === "waitlisted" ? await crewNextWaitlistPosition(operation.id) : null,
|
|
245
|
+
waitlistReason: status2 === "waitlisted" ? "capacity" : null,
|
|
246
|
+
addedBy: null
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
const status = entry.member.crewStatus;
|
|
250
|
+
const dto = await crewMemberDtoOf(entry, operation, true);
|
|
251
|
+
await crewFire(event, "crew:member:applied", operation.id, dto);
|
|
252
|
+
if (status === "waitlisted") await crewFire(event, "crew:member:waitlisted", operation.id, dto);
|
|
253
|
+
await crewNotify(event, entry, operation, settings, status);
|
|
254
|
+
return { entry, created: true };
|
|
255
|
+
}
|
|
256
|
+
export async function crewDecide(event, id, to, options = { by: null }) {
|
|
257
|
+
const entry = await getCrewEntryOr404(id);
|
|
258
|
+
const from = entry.member.crewStatus;
|
|
259
|
+
const operationId = entry.team.ownerId;
|
|
260
|
+
const operation = await getOperationOr404(operationId);
|
|
261
|
+
const settings = await findCrewSettings(operationId);
|
|
262
|
+
let team = entry.team;
|
|
263
|
+
let role = entry.member.role;
|
|
264
|
+
if (options.teamId && options.teamId !== team.id) {
|
|
265
|
+
team = await getCrewOperationTeamOr400(operationId, options.teamId);
|
|
266
|
+
if (await findTeamMember(team.id, entry.member.userId)) throw conflict("This person is on that team already.", { field: "teamId" });
|
|
267
|
+
const keys = ladderKeys((await loadTeamLadders(event))[team.kind]);
|
|
268
|
+
if (!keys.includes(role)) role = keys[0] ?? role;
|
|
269
|
+
}
|
|
270
|
+
if (options.role) role = options.role;
|
|
271
|
+
const retargets = team.id !== entry.team.id || role !== entry.member.role;
|
|
272
|
+
if (retargets) await assertTeamRoleOnLadder(event, team.kind, role);
|
|
273
|
+
const moves = from !== to;
|
|
274
|
+
if (!moves && !retargets) return entry;
|
|
275
|
+
if (moves && !canMoveCrew(from, to)) throw apiError(409, "crew_invalid_transition", `A person who is ${from} cannot be ${to}.`);
|
|
276
|
+
const result = await crewUpdateMember(event, entry, {
|
|
277
|
+
crewStatus: to,
|
|
278
|
+
decidedAt: /* @__PURE__ */ new Date(),
|
|
279
|
+
decidedBy: options.by,
|
|
280
|
+
// Staff waitlisting is sticky: only people waitlisted because the crew was full are promoted automatically.
|
|
281
|
+
waitlistPosition: to === "waitlisted" ? from === "waitlisted" ? entry.member.waitlistPosition : await crewNextWaitlistPosition(operationId) : null,
|
|
282
|
+
waitlistReason: to === "waitlisted" ? from === "waitlisted" ? entry.member.waitlistReason : "manual" : null,
|
|
283
|
+
...team.id !== entry.team.id ? { teamId: team.id } : {},
|
|
284
|
+
...role !== entry.member.role ? { role } : {}
|
|
285
|
+
});
|
|
286
|
+
if (!result.changed || !moves) return result.entry;
|
|
287
|
+
const updated = result.entry;
|
|
288
|
+
await crewFire(event, `crew:member:${to}`, operationId, await crewMemberDtoOf(updated, operation, true));
|
|
289
|
+
await crewNotify(event, updated, operation, settings, to);
|
|
290
|
+
if (from === "approved") await crewPromoteWaitlist(event, operation, settings);
|
|
291
|
+
return updated;
|
|
292
|
+
}
|
|
293
|
+
export async function crewAddMember(event, input) {
|
|
294
|
+
const { operation, userId, by } = input;
|
|
295
|
+
const existing = await findCrewEntry(userId, operation.id);
|
|
296
|
+
const status = existing?.member.crewStatus;
|
|
297
|
+
if (existing && status && status !== "withdrawn" && status !== "rejected") {
|
|
298
|
+
throw conflict(`This person is ${status} in this crew already.`, { field: "userId", status, memberId: existing.member.id });
|
|
299
|
+
}
|
|
300
|
+
if (!existing && await crewHasPlainMembership(userId, operation.id)) {
|
|
301
|
+
throw conflict("This person is on a team of this operation already.", { field: "userId", status: null });
|
|
302
|
+
}
|
|
303
|
+
const settings = await findCrewSettings(operation.id);
|
|
304
|
+
const team = input.teamId ? await getCrewOperationTeamOr400(operation.id, input.teamId) : existing?.team ?? await crewDefaultTeamOf(event, operation.id, settings);
|
|
305
|
+
let role = input.role ?? (existing && existing.team.id === team.id ? existing.member.role : void 0);
|
|
306
|
+
if (role) await assertTeamRoleOnLadder(event, team.kind, role);
|
|
307
|
+
else role = await crewLowestRole(event, team.kind);
|
|
308
|
+
const now = /* @__PURE__ */ new Date();
|
|
309
|
+
const decided = { crewStatus: "approved", decidedAt: now, decidedBy: by, waitlistPosition: null, waitlistReason: null };
|
|
310
|
+
let entry;
|
|
311
|
+
if (existing) {
|
|
312
|
+
if (team.id !== existing.team.id && await findTeamMember(team.id, userId)) throw conflict("This person is on that team already.", { field: "teamId" });
|
|
313
|
+
const result = await crewUpdateMember(event, existing, {
|
|
314
|
+
...decided,
|
|
315
|
+
...team.id !== existing.team.id ? { teamId: team.id } : {},
|
|
316
|
+
...role !== existing.member.role ? { role } : {}
|
|
317
|
+
});
|
|
318
|
+
if (!result.changed) {
|
|
319
|
+
const current = result.entry.member.crewStatus;
|
|
320
|
+
throw conflict(`This person is ${current} in this crew already.`, { field: "userId", status: current, memberId: result.entry.member.id });
|
|
321
|
+
}
|
|
322
|
+
entry = result.entry;
|
|
323
|
+
} else {
|
|
324
|
+
entry = await crewInsertMember(event, { team, userId, role, appliedAt: now, addedBy: by, locale: null, ...decided });
|
|
325
|
+
}
|
|
326
|
+
await crewFire(event, "crew:member:approved", operation.id, await crewMemberDtoOf(entry, operation, true));
|
|
327
|
+
await crewNotify(event, entry, operation, settings, "approved");
|
|
328
|
+
return { entry, created: !existing };
|
|
329
|
+
}
|
|
330
|
+
export async function crewApproveMany(event, operationId, ids, by) {
|
|
331
|
+
const approved = [];
|
|
332
|
+
const skipped = [];
|
|
333
|
+
for (const id of ids) {
|
|
334
|
+
const entry = await getCrewEntryOr404(id).catch(() => void 0);
|
|
335
|
+
if (!entry || entry.team.ownerId !== operationId || entry.member.crewStatus === "approved") {
|
|
336
|
+
skipped.push(id);
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
try {
|
|
340
|
+
approved.push(await crewDecide(event, id, "approved", { by }));
|
|
341
|
+
} catch (error) {
|
|
342
|
+
const status = error.statusCode;
|
|
343
|
+
if (typeof status === "number" && status >= 400 && status < 500) skipped.push(id);
|
|
344
|
+
else throw error;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return { approved, skipped };
|
|
348
|
+
}
|
|
349
|
+
export async function crewMove(event, id, input) {
|
|
350
|
+
const entry = await getCrewEntryOr404(id);
|
|
351
|
+
const operationId = entry.team.ownerId;
|
|
352
|
+
let team = entry.team;
|
|
353
|
+
let role = entry.member.role;
|
|
354
|
+
if (input.teamId && input.teamId !== team.id) {
|
|
355
|
+
team = await getCrewOperationTeamOr400(operationId, input.teamId);
|
|
356
|
+
if (await findTeamMember(team.id, entry.member.userId)) throw conflict("This person is on that team already.", { field: "teamId" });
|
|
357
|
+
const keys = ladderKeys((await loadTeamLadders(event))[team.kind]);
|
|
358
|
+
if (!keys.includes(role)) role = keys[0] ?? role;
|
|
359
|
+
}
|
|
360
|
+
if (input.role) role = input.role;
|
|
361
|
+
await assertTeamRoleOnLadder(event, team.kind, role);
|
|
362
|
+
if (team.id === entry.team.id && role === entry.member.role) return entry;
|
|
363
|
+
return (await crewUpdateMember(event, entry, {
|
|
364
|
+
...team.id !== entry.team.id ? { teamId: team.id } : {},
|
|
365
|
+
...role !== entry.member.role ? { role } : {}
|
|
366
|
+
})).entry;
|
|
367
|
+
}
|
|
368
|
+
export async function crewPromoteWaitlist(event, operation, settings) {
|
|
369
|
+
if (!settings || settings.capacity === null || !settings.waitlist) return [];
|
|
370
|
+
const promoted = [];
|
|
371
|
+
for (let guard = 0; guard < 500; guard++) {
|
|
372
|
+
if (await crewApprovedCount(operation.id) >= settings.capacity) break;
|
|
373
|
+
const previous = sql`case when ${crewPreviousExists(operation, schema.teamMembers.userId)} then 1 else 0 end`;
|
|
374
|
+
const [next] = await db.select({ id: schema.teamMembers.id }).from(schema.teamMembers).innerJoin(schema.teams, eq(schema.teamMembers.teamId, schema.teams.id)).where(and(operationTeamOwned(operation.id), eq(schema.teamMembers.crewStatus, "waitlisted"), eq(schema.teamMembers.waitlistReason, "capacity"))).orderBy(...settings.previousCrewFirst ? [desc(previous)] : [], asc(schema.teamMembers.waitlistPosition), asc(schema.teamMembers.appliedAt), asc(schema.teamMembers.id)).limit(1);
|
|
375
|
+
if (!next) break;
|
|
376
|
+
const candidate = await getCrewEntryOr404(next.id).catch(() => void 0);
|
|
377
|
+
if (!candidate || candidate.member.crewStatus !== "waitlisted") continue;
|
|
378
|
+
const claimed = await crewWrite(event, candidate, { crewStatus: "approved", decidedAt: /* @__PURE__ */ new Date(), decidedBy: null, waitlistPosition: null, waitlistReason: null });
|
|
379
|
+
if (!claimed) continue;
|
|
380
|
+
if (await crewApprovedCount(operation.id) > settings.capacity) {
|
|
381
|
+
await crewWrite(event, claimed, {
|
|
382
|
+
crewStatus: "waitlisted",
|
|
383
|
+
decidedAt: candidate.member.decidedAt,
|
|
384
|
+
decidedBy: candidate.member.decidedBy,
|
|
385
|
+
waitlistPosition: candidate.member.waitlistPosition,
|
|
386
|
+
waitlistReason: "capacity"
|
|
387
|
+
});
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
await crewFireTeamsHooks(event, candidate, claimed);
|
|
391
|
+
await crewFire(event, "crew:member:approved", operation.id, await crewMemberDtoOf(claimed, operation, true));
|
|
392
|
+
await crewNotify(event, claimed, operation, settings, "approved");
|
|
393
|
+
promoted.push(claimed);
|
|
394
|
+
}
|
|
395
|
+
return promoted;
|
|
396
|
+
}
|
|
397
|
+
const gone = (code, message) => apiError(410, code, message);
|
|
398
|
+
export function crewOperationEnded(operation, now = /* @__PURE__ */ new Date()) {
|
|
399
|
+
if (operation.status === "archived" || operation.status === "wrapped_up") return true;
|
|
400
|
+
return !!operation.endsAt && operation.endsAt.getTime() <= now.getTime();
|
|
401
|
+
}
|
|
402
|
+
export async function crewAcceptInvite(event, token, userId) {
|
|
403
|
+
const tokenHash = await crewHashToken(token);
|
|
404
|
+
const [invite] = await db.select().from(schema.crewInvites).where(eq(schema.crewInvites.tokenHash, tokenHash)).limit(1);
|
|
405
|
+
if (!invite) throw notFound("This invite link is not valid.");
|
|
406
|
+
const reasonOf = (row) => row.revokedAt ? gone("invite_revoked", "This invite link was revoked.") : row.expiresAt && row.expiresAt.getTime() <= Date.now() ? gone("invite_expired", "This invite link has expired.") : gone("invite_used", "This invite link has been used up.");
|
|
407
|
+
if (invite.revokedAt || invite.expiresAt && invite.expiresAt.getTime() <= Date.now() || invite.uses >= invite.maxUses) throw reasonOf(invite);
|
|
408
|
+
const team = await findCrewOperationTeam(invite.operationId, invite.teamId);
|
|
409
|
+
if (!team) throw gone("invite_unavailable", "The team of this invite no longer exists.");
|
|
410
|
+
const operation = await getOperationOr404(invite.operationId);
|
|
411
|
+
if (crewOperationEnded(operation)) throw gone("invite_unavailable", "This operation has ended, so the invite link no longer works.");
|
|
412
|
+
await assertTeamRoleOnLadder(event, team.kind, invite.role);
|
|
413
|
+
const existing = await findCrewEntry(userId, invite.operationId);
|
|
414
|
+
const atTeam = await findTeamMember(team.id, userId);
|
|
415
|
+
if (existing?.member.crewStatus === "approved" || await crewHasPlainMembership(userId, invite.operationId)) {
|
|
416
|
+
throw apiError(409, "crew_already_member", "You are on this crew already.");
|
|
417
|
+
}
|
|
418
|
+
if (existing?.member.crewStatus === "rejected") throw apiError(409, "crew_rejected", "Your application was rejected, so an invite cannot add you.");
|
|
419
|
+
const now = /* @__PURE__ */ new Date();
|
|
420
|
+
const claimed = await db.update(schema.crewInvites).set({ uses: sql`${schema.crewInvites.uses} + 1` }).where(and(
|
|
421
|
+
eq(schema.crewInvites.id, invite.id),
|
|
422
|
+
isNull(schema.crewInvites.revokedAt),
|
|
423
|
+
lt(schema.crewInvites.uses, schema.crewInvites.maxUses),
|
|
424
|
+
or(isNull(schema.crewInvites.expiresAt), gt(schema.crewInvites.expiresAt, now))
|
|
425
|
+
)).returning({ id: schema.crewInvites.id });
|
|
426
|
+
if (!claimed.length) {
|
|
427
|
+
const [fresh] = await db.select().from(schema.crewInvites).where(eq(schema.crewInvites.id, invite.id)).limit(1);
|
|
428
|
+
throw fresh ? reasonOf(fresh) : notFound("This invite link is not valid.");
|
|
429
|
+
}
|
|
430
|
+
const base = atTeam ? { member: atTeam, team } : existing;
|
|
431
|
+
const decided = { crewStatus: "approved", decidedAt: now, decidedBy: invite.createdBy ?? null, waitlistPosition: null, waitlistReason: null };
|
|
432
|
+
let entry;
|
|
433
|
+
try {
|
|
434
|
+
if (base) {
|
|
435
|
+
const result = await crewWrite(event, base, {
|
|
436
|
+
...decided,
|
|
437
|
+
role: invite.role,
|
|
438
|
+
...base.team.id !== team.id ? { teamId: team.id } : {},
|
|
439
|
+
...base.member.crewStatus ? {} : { appliedAt: now },
|
|
440
|
+
...base.member.locale ? {} : { locale: crewRequestLocale(event) ?? null }
|
|
441
|
+
});
|
|
442
|
+
if (!result) throw apiError(409, "conflict", "Your crew status changed meanwhile. Try the link again.");
|
|
443
|
+
entry = result;
|
|
444
|
+
if (existing && existing.member.id !== base.member.id) {
|
|
445
|
+
await crewWrite(event, existing, { crewStatus: "withdrawn", decidedAt: now, decidedBy: null, waitlistPosition: null, waitlistReason: null });
|
|
446
|
+
}
|
|
447
|
+
} else {
|
|
448
|
+
entry = await crewInsertMember(event, { team, userId, role: invite.role, appliedAt: now, addedBy: invite.createdBy ?? null, locale: crewRequestLocale(event) ?? null, fire: false, ...decided });
|
|
449
|
+
}
|
|
450
|
+
} catch (error) {
|
|
451
|
+
await db.update(schema.crewInvites).set({ uses: sql`${schema.crewInvites.uses} - 1` }).where(eq(schema.crewInvites.id, invite.id));
|
|
452
|
+
throw error;
|
|
453
|
+
}
|
|
454
|
+
const settings = await findCrewSettings(operation.id);
|
|
455
|
+
await crewFireTeamsHooks(event, base, entry);
|
|
456
|
+
await crewFire(event, "crew:member:approved", operation.id, await crewMemberDtoOf(entry, operation, true));
|
|
457
|
+
await crewNotify(event, entry, operation, settings, "approved");
|
|
458
|
+
return toMyCrewDto({ member: entry.member, team: teamLite(entry.team), operation, roleLabel: await crewRoleLabelOf(event, entry.team.kind, entry.member.role) });
|
|
459
|
+
}
|
|
460
|
+
export async function crewLedTeamIds(event, userId, operationId) {
|
|
461
|
+
const led = [];
|
|
462
|
+
for (const team of await crewOperationTeams(operationId)) {
|
|
463
|
+
if (await isTeamLead(event, userId, team.id)) led.push(team.id);
|
|
464
|
+
}
|
|
465
|
+
return led;
|
|
466
|
+
}
|
|
467
|
+
export async function crewMembersPage(operation, query, scope) {
|
|
468
|
+
if (scope.teamIds && !scope.teamIds.length) return paginate({ items: [], total: 0, query });
|
|
469
|
+
const previous = crewPreviousExists(operation, schema.teamMembers.userId);
|
|
470
|
+
const search = query.q ? or(searchContains(schema.users.searchText, query.q), scope.pii ? likeContains(schema.users.email, query.q) : void 0) : void 0;
|
|
471
|
+
const where = and(
|
|
472
|
+
operationTeamOwned(operation.id),
|
|
473
|
+
isNotNull(schema.teamMembers.crewStatus),
|
|
474
|
+
query.status ? eq(schema.teamMembers.crewStatus, query.status) : void 0,
|
|
475
|
+
query.team ? eq(schema.teamMembers.teamId, query.team) : void 0,
|
|
476
|
+
query.previous === "true" ? previous : query.previous === "false" ? not(previous) : void 0,
|
|
477
|
+
scope.teamIds ? inArray(schema.teamMembers.teamId, scope.teamIds) : void 0,
|
|
478
|
+
search
|
|
479
|
+
);
|
|
480
|
+
const previousFlag = sql`case when ${previous} then 1 else 0 end`;
|
|
481
|
+
const [rows, [totals]] = await Promise.all([
|
|
482
|
+
db.select({
|
|
483
|
+
member: schema.teamMembers,
|
|
484
|
+
team: { id: schema.teams.id, name: schema.teams.name, ownerId: schema.teams.ownerId },
|
|
485
|
+
user: { name: schema.users.name, email: schema.users.email, avatar: schema.users.avatar, phone: schema.users.phone },
|
|
486
|
+
previous: previousFlag
|
|
487
|
+
}).from(schema.teamMembers).innerJoin(schema.teams, eq(schema.teamMembers.teamId, schema.teams.id)).leftJoin(schema.users, eq(schema.users.id, schema.teamMembers.userId)).where(where).orderBy(...orderByQuery({
|
|
488
|
+
appliedAt: schema.teamMembers.appliedAt,
|
|
489
|
+
name: schema.users.name,
|
|
490
|
+
crewStatus: schema.teamMembers.crewStatus,
|
|
491
|
+
role: schema.teamMembers.role,
|
|
492
|
+
previousCrew: previousFlag,
|
|
493
|
+
waitlistPosition: schema.teamMembers.waitlistPosition
|
|
494
|
+
}, query, { tiebreaker: schema.teamMembers.id })).limit(pageRange(query).limit).offset(pageRange(query).offset),
|
|
495
|
+
db.select({ total: count() }).from(schema.teamMembers).innerJoin(schema.teams, eq(schema.teamMembers.teamId, schema.teams.id)).leftJoin(schema.users, eq(schema.users.id, schema.teamMembers.userId)).where(where)
|
|
496
|
+
]);
|
|
497
|
+
return paginate({
|
|
498
|
+
items: rows.map((row) => toCrewMemberDto({ member: row.member, team: row.team, user: row.user, previous: Number(row.previous) === 1, pii: scope.pii })),
|
|
499
|
+
total: Number(totals?.total ?? 0),
|
|
500
|
+
query
|
|
501
|
+
});
|
|
502
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export declare const crewApi: {
|
|
2
|
+
readonly me: {
|
|
3
|
+
/** `GET` my crew status in every operation I applied to or work at. */
|
|
4
|
+
readonly crew: "/api/crew/me";
|
|
5
|
+
};
|
|
6
|
+
/** `GET` what the apply page needs for an operation (its id or URL name): the form to render, or why not. */
|
|
7
|
+
readonly apply: (operation: string) => string;
|
|
8
|
+
/** `GET` (paginated) an operation's crew as the caller may read it: staff all of it, team leads their own teams, others nothing. */
|
|
9
|
+
readonly members: (operationId: string) => string;
|
|
10
|
+
/** `GET` one crew member: yourself, a lead of the team, or staff. */
|
|
11
|
+
readonly member: (id: string) => string;
|
|
12
|
+
/** `POST` to accept an invite link: joins the invite's team, approved, with the invite's role. */
|
|
13
|
+
readonly acceptInvite: (token: string) => string;
|
|
14
|
+
readonly admin: {
|
|
15
|
+
/** `GET` / `PUT` how an operation's crew is recruited. */
|
|
16
|
+
readonly settings: (operationId: string) => string;
|
|
17
|
+
/** `GET` (paginated, `?status&team&previous&q`) an operation's whole crew, for staff. */
|
|
18
|
+
readonly members: (operationId: string) => string;
|
|
19
|
+
/** `POST` `{ userId, teamId?, role? }` to add a person to the crew, approved at once (201; 200 when a withdrawn or rejected row is reused). */
|
|
20
|
+
readonly addMember: (operationId: string) => string;
|
|
21
|
+
/** `POST` `{ ids }` to approve several people at once. */
|
|
22
|
+
readonly approveMany: (operationId: string) => string;
|
|
23
|
+
/** `PATCH` `{ teamId?, role? }` to move a crew member. */
|
|
24
|
+
readonly member: (id: string) => string;
|
|
25
|
+
/** `POST` `{ teamId?, role? }` to approve, optionally into another team or role. */
|
|
26
|
+
readonly approve: (id: string) => string;
|
|
27
|
+
/** `POST` to reject. */
|
|
28
|
+
readonly reject: (id: string) => string;
|
|
29
|
+
/** `POST` to mark a person who stepped down as withdrawn (volunteers cannot withdraw themselves). */
|
|
30
|
+
readonly withdraw: (id: string) => string;
|
|
31
|
+
/** `POST` to put on the waiting list. */
|
|
32
|
+
readonly waitlist: (id: string) => string;
|
|
33
|
+
/** `GET` the application's answers (needs `crew:pii`). */
|
|
34
|
+
readonly submission: (id: string) => string;
|
|
35
|
+
/** `GET` `?q=` users to add to a crew (at most 20; emails and email matches only with `crew:pii`). */
|
|
36
|
+
readonly users: "/api/admin/crew/users";
|
|
37
|
+
/** `GET` the forms an operation can recruit with (`?operationId=` keeps its own). */
|
|
38
|
+
readonly forms: "/api/admin/crew/forms";
|
|
39
|
+
/** `GET` / `POST` an operation's invite links. */
|
|
40
|
+
readonly invites: (operationId: string) => string;
|
|
41
|
+
/** `DELETE` (revoke) one invite. */
|
|
42
|
+
readonly invite: (id: string) => string;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const enc = encodeURIComponent;
|
|
2
|
+
const BASE = "/api/crew";
|
|
3
|
+
const ADMIN_BASE = "/api/admin/crew";
|
|
4
|
+
export const crewApi = {
|
|
5
|
+
me: {
|
|
6
|
+
/** `GET` my crew status in every operation I applied to or work at. */
|
|
7
|
+
crew: `${BASE}/me`
|
|
8
|
+
},
|
|
9
|
+
/** `GET` what the apply page needs for an operation (its id or URL name): the form to render, or why not. */
|
|
10
|
+
apply: (operation) => `${BASE}/operations/${enc(operation)}/apply`,
|
|
11
|
+
/** `GET` (paginated) an operation's crew as the caller may read it: staff all of it, team leads their own teams, others nothing. */
|
|
12
|
+
members: (operationId) => `${BASE}/operations/${enc(operationId)}/members`,
|
|
13
|
+
/** `GET` one crew member: yourself, a lead of the team, or staff. */
|
|
14
|
+
member: (id) => `${BASE}/members/${enc(id)}`,
|
|
15
|
+
/** `POST` to accept an invite link: joins the invite's team, approved, with the invite's role. */
|
|
16
|
+
acceptInvite: (token) => `${BASE}/invites/${enc(token)}/accept`,
|
|
17
|
+
admin: {
|
|
18
|
+
/** `GET` / `PUT` how an operation's crew is recruited. */
|
|
19
|
+
settings: (operationId) => `${ADMIN_BASE}/operations/${enc(operationId)}/settings`,
|
|
20
|
+
/** `GET` (paginated, `?status&team&previous&q`) an operation's whole crew, for staff. */
|
|
21
|
+
members: (operationId) => `${ADMIN_BASE}/operations/${enc(operationId)}/members`,
|
|
22
|
+
/** `POST` `{ userId, teamId?, role? }` to add a person to the crew, approved at once (201; 200 when a withdrawn or rejected row is reused). */
|
|
23
|
+
addMember: (operationId) => `${ADMIN_BASE}/operations/${enc(operationId)}/members`,
|
|
24
|
+
/** `POST` `{ ids }` to approve several people at once. */
|
|
25
|
+
approveMany: (operationId) => `${ADMIN_BASE}/operations/${enc(operationId)}/members/approve`,
|
|
26
|
+
/** `PATCH` `{ teamId?, role? }` to move a crew member. */
|
|
27
|
+
member: (id) => `${ADMIN_BASE}/members/${enc(id)}`,
|
|
28
|
+
/** `POST` `{ teamId?, role? }` to approve, optionally into another team or role. */
|
|
29
|
+
approve: (id) => `${ADMIN_BASE}/members/${enc(id)}/approve`,
|
|
30
|
+
/** `POST` to reject. */
|
|
31
|
+
reject: (id) => `${ADMIN_BASE}/members/${enc(id)}/reject`,
|
|
32
|
+
/** `POST` to mark a person who stepped down as withdrawn (volunteers cannot withdraw themselves). */
|
|
33
|
+
withdraw: (id) => `${ADMIN_BASE}/members/${enc(id)}/withdraw`,
|
|
34
|
+
/** `POST` to put on the waiting list. */
|
|
35
|
+
waitlist: (id) => `${ADMIN_BASE}/members/${enc(id)}/waitlist`,
|
|
36
|
+
/** `GET` the application's answers (needs `crew:pii`). */
|
|
37
|
+
submission: (id) => `${ADMIN_BASE}/members/${enc(id)}/submission`,
|
|
38
|
+
/** `GET` `?q=` users to add to a crew (at most 20; emails and email matches only with `crew:pii`). */
|
|
39
|
+
users: `${ADMIN_BASE}/users`,
|
|
40
|
+
/** `GET` the forms an operation can recruit with (`?operationId=` keeps its own). */
|
|
41
|
+
forms: `${ADMIN_BASE}/forms`,
|
|
42
|
+
/** `GET` / `POST` an operation's invite links. */
|
|
43
|
+
invites: (operationId) => `${ADMIN_BASE}/operations/${enc(operationId)}/invites`,
|
|
44
|
+
/** `DELETE` (revoke) one invite. */
|
|
45
|
+
invite: (id) => `${ADMIN_BASE}/invites/${enc(id)}`
|
|
46
|
+
}
|
|
47
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { translate } from '@frigear-nu/ui/translated';
|
|
2
|
+
export { translate };
|
|
3
|
+
export * from './api.js';
|
|
4
|
+
export * from './links.js';
|
|
5
|
+
export * from './status.js';
|
|
6
|
+
export type { CreatedCrewInviteDto, CrewApplyDto, CrewFormOptionDto, CrewInviteDto, CrewMemberContext, CrewMemberDto, CrewSettingsDto, CrewStatus, CrewStatusEmail, CrewStatusEmails, CrewSubmissionDto, ModuleOptions, MyCrewDto, Translated, } from '../../types.js';
|
|
7
|
+
/** Random, prefixed id (`<prefix>_<32 hex>`): the one of `@frigear-nu/module-kit/id`. */
|
|
8
|
+
export { newId } from '@frigear-nu/module-kit/id';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** The configured page paths (`public.crew.pages`): `apply` with an `:operationSlug` param, `invite` with a `:token` param. */
|
|
2
|
+
export type CrewPages = {
|
|
3
|
+
apply?: string;
|
|
4
|
+
status?: string;
|
|
5
|
+
invite?: string;
|
|
6
|
+
};
|
|
7
|
+
/** The default page paths, as `pages.apply`, `pages.status` and `pages.invite` resolve `true`. */
|
|
8
|
+
export declare const CREW_DEFAULT_PAGES: {
|
|
9
|
+
readonly home: "/crew";
|
|
10
|
+
readonly apply: "/crew/apply/:operationSlug";
|
|
11
|
+
readonly status: "/crew/status";
|
|
12
|
+
readonly invite: "/crew/invite/:token";
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Builds every link to a crew page. All of them go through `localize` (the active locale's prefix, see `useLocaleLink`
|
|
16
|
+
* of `@frigear-nu/ui`). A page that is turned off still gets a link on its default path, so a caller decides whether
|
|
17
|
+
* to show it from `pages`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function createCrewLinks(pages: CrewPages | undefined, localize: (path: string) => string): {
|
|
20
|
+
apply: (operationSlug: string) => string;
|
|
21
|
+
status: () => string;
|
|
22
|
+
invite: (token: string) => string;
|
|
23
|
+
};
|
|
24
|
+
/** How long an invite link lasts when staff set no expiry: 30 days. */
|
|
25
|
+
export declare const CREW_INVITE_DEFAULT_DAYS = 30;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const CREW_DEFAULT_PAGES = {
|
|
2
|
+
home: "/crew",
|
|
3
|
+
apply: "/crew/apply/:operationSlug",
|
|
4
|
+
status: "/crew/status",
|
|
5
|
+
invite: "/crew/invite/:token"
|
|
6
|
+
};
|
|
7
|
+
export function createCrewLinks(pages, localize) {
|
|
8
|
+
return {
|
|
9
|
+
apply: (operationSlug) => localize((pages?.apply || CREW_DEFAULT_PAGES.apply).replace(":operationSlug", encodeURIComponent(operationSlug))),
|
|
10
|
+
status: () => localize(pages?.status || CREW_DEFAULT_PAGES.status),
|
|
11
|
+
invite: (token) => localize((pages?.invite || CREW_DEFAULT_PAGES.invite).replace(":token", encodeURIComponent(token)))
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export const CREW_INVITE_DEFAULT_DAYS = 30;
|