This commit is contained in:
122
src/actions/team.ts
Normal file
122
src/actions/team.ts
Normal file
@ -0,0 +1,122 @@
|
||||
import { defineAction } from 'astro:actions';
|
||||
import { Session } from '@util/session.ts';
|
||||
import { Permissions } from '@util/permissions.ts';
|
||||
import { db } from '@db/database.ts';
|
||||
import { z } from 'astro:schema';
|
||||
|
||||
export const team = {
|
||||
addTeam: defineAction({
|
||||
input: z.object({
|
||||
name: z.string(),
|
||||
color: z.string(),
|
||||
lastJoined: z.string().datetime().nullable(),
|
||||
memberOne: z.object({
|
||||
id: z.number().nullish(),
|
||||
username: z.string()
|
||||
}),
|
||||
memberTwo: z.object({
|
||||
id: z.number().nullish(),
|
||||
username: z.string()
|
||||
})
|
||||
}),
|
||||
handler: async (input, context) => {
|
||||
Session.actionSessionFromCookies(context.cookies, Permissions.Users);
|
||||
|
||||
const teamId = await db.transaction(async (tx) => {
|
||||
const team = await tx.addTeam({
|
||||
name: input.name,
|
||||
color: input.color
|
||||
});
|
||||
|
||||
await tx.addTeamDraft({
|
||||
memberOneName: input.memberOne.username,
|
||||
memberTwoName: input.memberTwo.username,
|
||||
teamId: team.id
|
||||
});
|
||||
|
||||
if (input.memberOne.id) {
|
||||
await tx.addTeamMember({
|
||||
teamId: team.id,
|
||||
userId: input.memberOne.id
|
||||
});
|
||||
}
|
||||
|
||||
if (input.memberTwo.id) {
|
||||
await tx.addTeamMember({
|
||||
teamId: team.id,
|
||||
userId: input.memberTwo.id
|
||||
});
|
||||
}
|
||||
|
||||
return team.id;
|
||||
});
|
||||
|
||||
return Object.assign(input, { id: teamId });
|
||||
}
|
||||
}),
|
||||
editTeam: defineAction({
|
||||
input: z.object({
|
||||
id: z.number(),
|
||||
name: z.string(),
|
||||
color: z.string(),
|
||||
lastJoined: z.string().datetime().nullable(),
|
||||
memberOne: z.object({
|
||||
id: z.number().nullable(),
|
||||
username: z.string()
|
||||
}),
|
||||
memberTwo: z.object({
|
||||
id: z.number().nullable(),
|
||||
username: z.string()
|
||||
})
|
||||
}),
|
||||
handler: async (input, context) => {
|
||||
Session.actionSessionFromCookies(context.cookies, Permissions.Users);
|
||||
|
||||
await db.transaction(async (tx) => {
|
||||
await tx.editTeam({
|
||||
id: input.id,
|
||||
name: input.name,
|
||||
color: input.color,
|
||||
lastJoined: input.lastJoined
|
||||
});
|
||||
|
||||
await tx.deleteTeamDraft({ teamId: input.id });
|
||||
await tx.deleteTeamMemberByTeamId({ teamId: input.id });
|
||||
|
||||
await tx.addTeamDraft({
|
||||
memberOneName: input.memberOne.username,
|
||||
memberTwoName: input.memberTwo.username,
|
||||
teamId: input.id
|
||||
});
|
||||
|
||||
if (input.memberOne.id) {
|
||||
await tx.addTeamMember({
|
||||
teamId: input.id,
|
||||
userId: input.memberOne.id
|
||||
});
|
||||
}
|
||||
|
||||
if (input.memberTwo.id) {
|
||||
await tx.addTeamMember({
|
||||
teamId: input.id,
|
||||
userId: input.memberTwo.id
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}),
|
||||
teams: defineAction({
|
||||
input: z.object({
|
||||
name: z.string().nullish(),
|
||||
username: z.string().nullish(),
|
||||
limit: z.number().optional()
|
||||
}),
|
||||
handler: async (input, context) => {
|
||||
Session.actionSessionFromCookies(context.cookies, Permissions.Users);
|
||||
|
||||
return {
|
||||
teams: await db.getTeams(input)
|
||||
};
|
||||
}
|
||||
})
|
||||
};
|
Reference in New Issue
Block a user