Skip to content

Commit 43abbef

Browse files
committed
WIP
1 parent 63bc618 commit 43abbef

39 files changed

Lines changed: 957 additions & 111 deletions

convex/_generated/api.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ import type * as _model_leagues_types from "../_model/leagues/types.js";
8282
import type * as _model_lists__helpers_checkListSubmittedOnTime from "../_model/lists/_helpers/checkListSubmittedOnTime.js";
8383
import type * as _model_lists__helpers_checkListVisibility from "../_model/lists/_helpers/checkListVisibility.js";
8484
import type * as _model_lists__helpers_deepenList from "../_model/lists/_helpers/deepenList.js";
85+
import type * as _model_lists__helpers_getAvailableActions from "../_model/lists/_helpers/getAvailableActions.js";
8586
import type * as _model_lists_index from "../_model/lists/index.js";
8687
import type * as _model_lists_mutations_createList from "../_model/lists/mutations/createList.js";
88+
import type * as _model_lists_mutations_deleteList from "../_model/lists/mutations/deleteList.js";
8789
import type * as _model_lists_mutations_toggleListApproval from "../_model/lists/mutations/toggleListApproval.js";
8890
import type * as _model_lists_mutations_updateList from "../_model/lists/mutations/updateList.js";
8991
import type * as _model_lists_queries_getList from "../_model/lists/queries/getList.js";
@@ -127,6 +129,7 @@ import type * as _model_photos_index from "../_model/photos/index.js";
127129
import type * as _model_photos_mutations_createPhoto from "../_model/photos/mutations/createPhoto.js";
128130
import type * as _model_photos_queries_getPhoto from "../_model/photos/queries/getPhoto.js";
129131
import type * as _model_photos_table from "../_model/photos/table.js";
132+
import type * as _model_tournamentCompetitors__helpers_checkUserIsTeamCaptain from "../_model/tournamentCompetitors/_helpers/checkUserIsTeamCaptain.js";
130133
import type * as _model_tournamentCompetitors__helpers_checkUsersAreTeammates from "../_model/tournamentCompetitors/_helpers/checkUsersAreTeammates.js";
131134
import type * as _model_tournamentCompetitors__helpers_deepenTournamentCompetitor from "../_model/tournamentCompetitors/_helpers/deepenTournamentCompetitor.js";
132135
import type * as _model_tournamentCompetitors__helpers_getAvailableActions from "../_model/tournamentCompetitors/_helpers/getAvailableActions.js";
@@ -402,8 +405,10 @@ declare const fullApi: ApiFromModules<{
402405
"_model/lists/_helpers/checkListSubmittedOnTime": typeof _model_lists__helpers_checkListSubmittedOnTime;
403406
"_model/lists/_helpers/checkListVisibility": typeof _model_lists__helpers_checkListVisibility;
404407
"_model/lists/_helpers/deepenList": typeof _model_lists__helpers_deepenList;
408+
"_model/lists/_helpers/getAvailableActions": typeof _model_lists__helpers_getAvailableActions;
405409
"_model/lists/index": typeof _model_lists_index;
406410
"_model/lists/mutations/createList": typeof _model_lists_mutations_createList;
411+
"_model/lists/mutations/deleteList": typeof _model_lists_mutations_deleteList;
407412
"_model/lists/mutations/toggleListApproval": typeof _model_lists_mutations_toggleListApproval;
408413
"_model/lists/mutations/updateList": typeof _model_lists_mutations_updateList;
409414
"_model/lists/queries/getList": typeof _model_lists_queries_getList;
@@ -447,6 +452,7 @@ declare const fullApi: ApiFromModules<{
447452
"_model/photos/mutations/createPhoto": typeof _model_photos_mutations_createPhoto;
448453
"_model/photos/queries/getPhoto": typeof _model_photos_queries_getPhoto;
449454
"_model/photos/table": typeof _model_photos_table;
455+
"_model/tournamentCompetitors/_helpers/checkUserIsTeamCaptain": typeof _model_tournamentCompetitors__helpers_checkUserIsTeamCaptain;
450456
"_model/tournamentCompetitors/_helpers/checkUsersAreTeammates": typeof _model_tournamentCompetitors__helpers_checkUsersAreTeammates;
451457
"_model/tournamentCompetitors/_helpers/deepenTournamentCompetitor": typeof _model_tournamentCompetitors__helpers_deepenTournamentCompetitor;
452458
"_model/tournamentCompetitors/_helpers/getAvailableActions": typeof _model_tournamentCompetitors__helpers_getAvailableActions;

convex/_model/lists/_helpers/deepenList.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Doc } from '../../../_generated/dataModel';
22
import { QueryCtx } from '../../../_generated/server';
33
import { checkListSubmittedOnTime } from './checkListSubmittedOnTime';
4+
import { getAvailableActions } from './getAvailableActions';
45

56
/* eslint-disable @typescript-eslint/explicit-function-return-type */
67
/**
@@ -18,6 +19,8 @@ export const deepenList = async (
1819
doc: Doc<'lists'>,
1920
) => ({
2021
...doc,
22+
displayName: '', // Future: Set displayName based on extracted list data
23+
availableActions: await getAvailableActions(ctx, doc),
2124
onTime: await checkListSubmittedOnTime(ctx, doc),
2225
});
2326
// FUTURE: Deepen list based on game system
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { getAuthUserId } from '@convex-dev/auth/server';
2+
3+
import { Doc } from '../../../_generated/dataModel';
4+
import { QueryCtx } from '../../../_generated/server';
5+
import { getDocStrict } from '../../common/_helpers/getDocStrict';
6+
import { checkUserIsTeamCaptain } from '../../tournamentCompetitors';
7+
import { checkUserIsTournamentOrganizer } from '../../tournamentOrganizers';
8+
9+
export enum ListActionKey {
10+
/** Delete this list. */
11+
Delete = 'delete',
12+
/** Download this list's file. */
13+
Download = 'download',
14+
/** Edit this list. */
15+
Manage = 'manage',
16+
}
17+
18+
/**
19+
* Gets a list of list actions which are available to the current user.
20+
*
21+
* @param ctx - Convex query context
22+
* @param doc - Raw list document
23+
* @returns An array of ListActionKey(s)
24+
*/
25+
export const getAvailableActions = async (
26+
ctx: QueryCtx,
27+
doc: Doc<'lists'>,
28+
): Promise<ListActionKey[]> => {
29+
const userId = await getAuthUserId(ctx);
30+
31+
const isSelf = userId && doc.userId === userId;
32+
33+
let isArchived = false;
34+
let isOrganizer = false;
35+
let isCaptain = false;
36+
37+
if (doc.tournamentRegistrationId) {
38+
const registration = await getDocStrict(ctx, doc.tournamentRegistrationId);
39+
const tournament = await getDocStrict(ctx, registration.tournamentId);
40+
41+
isOrganizer = await checkUserIsTournamentOrganizer(ctx, registration.tournamentId, userId);
42+
isCaptain = await checkUserIsTeamCaptain(ctx, registration.tournamentCompetitorId, userId);
43+
isArchived = tournament.status === 'archived';
44+
}
45+
46+
// ---- PRIMARY ACTIONS ----
47+
const actions: ListActionKey[] = [ListActionKey.Download];
48+
49+
if (isArchived) {
50+
return actions;
51+
}
52+
53+
if (isOrganizer || ((isSelf || isCaptain) && !doc.locked)) {
54+
actions.push(ListActionKey.Delete);
55+
actions.push(ListActionKey.Manage);
56+
}
57+
58+
return actions;
59+
};

convex/_model/lists/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ export type ListId = Id<'lists'>;
55
export {
66
type DeepList,
77
} from './_helpers/deepenList';
8+
export {
9+
ListActionKey,
10+
} from './_helpers/getAvailableActions';
811

912
// Mutations
1013
export {
1114
createList,
1215
createListArgs,
1316
} from './mutations/createList';
17+
export {
18+
deleteList,
19+
deleteListArgs,
20+
} from './mutations/deleteList';
1421
export {
1522
toggleListApproval,
1623
toggleListApprovalArgs,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Infer, v } from 'convex/values';
2+
3+
import { MutationCtx } from '../../../_generated/server';
4+
5+
export const deleteListArgs = v.object({
6+
id: v.id('lists'),
7+
});
8+
9+
export const deleteList = async (
10+
ctx: MutationCtx,
11+
args: Infer<typeof deleteListArgs>,
12+
): Promise<void> => {
13+
await ctx.db.delete(args.id);
14+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Id } from '../../../_generated/dataModel';
2+
import { QueryCtx } from '../../../_generated/server';
3+
import { getDocStrict } from '../../common/_helpers/getDocStrict';
4+
5+
export const checkUserIsTeamCaptain = async (
6+
ctx: QueryCtx,
7+
tournamentCompetitorId: Id<'tournamentCompetitors'>,
8+
userId: Id<'users'> | null,
9+
): Promise<boolean> => {
10+
if (!userId) {
11+
return false;
12+
}
13+
const { tournamentId, captainUserId } = await getDocStrict(ctx, tournamentCompetitorId);
14+
const { competitorSize } = await getDocStrict(ctx, tournamentId);
15+
if (competitorSize <= 1) {
16+
return false;
17+
}
18+
return captainUserId === userId;
19+
};

convex/_model/tournamentCompetitors/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export type TournamentCompetitorId = Id<'tournamentCompetitors'>;
77
export type ScoreAdjustment = Infer<typeof scoreAdjustment>;
88

99
// Helpers
10+
export {
11+
checkUserIsTeamCaptain,
12+
} from './_helpers/checkUserIsTeamCaptain';
1013
export {
1114
deepenTournamentCompetitor,
1215
type DeepTournamentCompetitor,

convex/_model/tournamentRegistrations/_helpers/getAvailableActions.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ export enum TournamentRegistrationActionKey {
1313
/** Delete this TournamentRegistration (and TournamentCompetitor if no remaining players). */
1414
Delete = 'delete',
1515

16+
/** Create a new list for this TournamentRegistration. */
17+
CreateList = 'createList',
18+
1619
// TODO
1720
// ApproveList = 'approveList',
18-
21+
1922
// TODO
2023
// RejectList = 'rejectList',
2124

@@ -75,6 +78,10 @@ export const getAvailableActions = async (
7578
actions.push(TournamentRegistrationActionKey.Edit);
7679
}
7780

81+
if (isOrganizer || isCaptain || isSelf) {
82+
actions.push(TournamentRegistrationActionKey.CreateList);
83+
}
84+
7885
// if (isOrganizer) {
7986
// actions.push(TournamentRegistrationActionKey.ApproveList);
8087
// }

convex/lists.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ export const createList = mutation({
66
handler: model.createList,
77
});
88

9+
export const deleteList = mutation({
10+
args: model.deleteListArgs,
11+
handler: model.deleteList,
12+
});
13+
914
export const updateList = mutation({
1015
args: model.updateListArgs,
1116
handler: model.updateList,

0 commit comments

Comments
 (0)