181 lines
4.5 KiB
TypeScript
181 lines
4.5 KiB
TypeScript
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({ local: true }).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
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}),
|
|
deleteTeam: defineAction({
|
|
input: z.object({
|
|
id: z.number()
|
|
}),
|
|
handler: async (input, context) => {
|
|
Session.actionSessionFromCookies(context.cookies, Permissions.Users);
|
|
|
|
await db.deleteTeam(input);
|
|
}
|
|
}),
|
|
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)
|
|
};
|
|
}
|
|
}),
|
|
addDeath: defineAction({
|
|
input: z.object({
|
|
deadUserId: z.number(),
|
|
killerUserId: z.number().nullish(),
|
|
message: z.string()
|
|
}),
|
|
handler: async (input, context) => {
|
|
Session.actionSessionFromCookies(context.cookies, Permissions.Users);
|
|
|
|
const { id } = await db.addDeath(input);
|
|
|
|
return {
|
|
id: id
|
|
};
|
|
}
|
|
}),
|
|
editDeath: defineAction({
|
|
input: z.object({
|
|
id: z.number(),
|
|
deadUserId: z.number(),
|
|
killerUserId: z.number().nullish(),
|
|
message: z.string()
|
|
}),
|
|
handler: async (input, context) => {
|
|
Session.actionSessionFromCookies(context.cookies, Permissions.Users);
|
|
|
|
await db.editDeath(input);
|
|
}
|
|
}),
|
|
deleteDeath: defineAction({
|
|
input: z.object({
|
|
id: z.number()
|
|
}),
|
|
handler: async (input, context) => {
|
|
Session.actionSessionFromCookies(context.cookies, Permissions.Users);
|
|
|
|
await db.deleteDeath(input);
|
|
}
|
|
}),
|
|
deaths: defineAction({
|
|
handler: async (_, context) => {
|
|
Session.actionSessionFromCookies(context.cookies, Permissions.Users);
|
|
|
|
return {
|
|
deaths: await db.getDeaths({})
|
|
};
|
|
}
|
|
})
|
|
};
|