191 lines
6.4 KiB
TypeScript
191 lines
6.4 KiB
TypeScript
'use server';
|
|
|
|
import { z } from 'zod';
|
|
|
|
import { authActionClient } from '@kit/next/safe-action';
|
|
import { getLogger } from '@kit/shared/logger';
|
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
|
|
|
import { isCourseDomainError } from '../../lib/errors';
|
|
import {
|
|
CreateCourseSchema,
|
|
UpdateCourseSchema,
|
|
EnrollParticipantSchema,
|
|
CreateSessionSchema,
|
|
CreateCategorySchema,
|
|
CreateInstructorSchema,
|
|
CreateLocationSchema,
|
|
} from '../../schema/course.schema';
|
|
import { createCourseManagementApi } from '../api';
|
|
|
|
export const createCourse = authActionClient
|
|
.inputSchema(CreateCourseSchema)
|
|
.action(async ({ parsedInput: input, ctx }) => {
|
|
const client = getSupabaseServerClient();
|
|
const logger = await getLogger();
|
|
const api = createCourseManagementApi(client);
|
|
|
|
logger.info({ name: 'course.create' }, 'Creating course...');
|
|
const result = await api.courses.create(input, ctx.user.id);
|
|
logger.info({ name: 'course.create' }, 'Course created');
|
|
return { success: true, data: result };
|
|
});
|
|
|
|
export const updateCourse = authActionClient
|
|
.inputSchema(UpdateCourseSchema)
|
|
.action(async ({ parsedInput: input, ctx }) => {
|
|
const client = getSupabaseServerClient();
|
|
const logger = await getLogger();
|
|
const api = createCourseManagementApi(client);
|
|
|
|
try {
|
|
logger.info({ name: 'course.update' }, 'Updating course...');
|
|
const result = await api.courses.update(input, ctx.user.id);
|
|
logger.info({ name: 'course.update' }, 'Course updated');
|
|
return { success: true, data: result };
|
|
} catch (e) {
|
|
if (isCourseDomainError(e)) {
|
|
return { success: false, error: e.message, code: e.code };
|
|
}
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
export const deleteCourse = authActionClient
|
|
.inputSchema(z.object({ courseId: z.string().uuid() }))
|
|
.action(async ({ parsedInput: input, ctx: _ctx }) => {
|
|
const client = getSupabaseServerClient();
|
|
const logger = await getLogger();
|
|
const api = createCourseManagementApi(client);
|
|
|
|
try {
|
|
logger.info({ name: 'course.delete' }, 'Archiving course...');
|
|
await api.courses.softDelete(input.courseId);
|
|
logger.info({ name: 'course.delete' }, 'Course archived');
|
|
return { success: true };
|
|
} catch (e) {
|
|
if (isCourseDomainError(e)) {
|
|
return { success: false, error: e.message, code: e.code };
|
|
}
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
export const enrollParticipant = authActionClient
|
|
.inputSchema(EnrollParticipantSchema)
|
|
.action(async ({ parsedInput: input, ctx: _ctx }) => {
|
|
const client = getSupabaseServerClient();
|
|
const logger = await getLogger();
|
|
const api = createCourseManagementApi(client);
|
|
|
|
try {
|
|
logger.info(
|
|
{ name: 'course.enrollParticipant' },
|
|
'Enrolling participant...',
|
|
);
|
|
const result = await api.enrollment.enroll(input);
|
|
logger.info({ name: 'course.enrollParticipant' }, 'Participant enrolled');
|
|
return { success: true, data: result };
|
|
} catch (e) {
|
|
if (isCourseDomainError(e)) {
|
|
return { success: false, error: e.message, code: e.code };
|
|
}
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
export const cancelEnrollment = authActionClient
|
|
.inputSchema(
|
|
z.object({
|
|
participantId: z.string().uuid(),
|
|
}),
|
|
)
|
|
.action(async ({ parsedInput: input, ctx: _ctx }) => {
|
|
const client = getSupabaseServerClient();
|
|
const logger = await getLogger();
|
|
const api = createCourseManagementApi(client);
|
|
|
|
logger.info(
|
|
{ name: 'course.cancelEnrollment' },
|
|
'Cancelling enrollment...',
|
|
);
|
|
await api.enrollment.cancel(input.participantId);
|
|
logger.info({ name: 'course.cancelEnrollment' }, 'Enrollment cancelled');
|
|
return { success: true };
|
|
});
|
|
|
|
export const markAttendance = authActionClient
|
|
.inputSchema(
|
|
z.object({
|
|
sessionId: z.string().uuid(),
|
|
participantId: z.string().uuid(),
|
|
present: z.boolean(),
|
|
}),
|
|
)
|
|
.action(async ({ parsedInput: input, ctx: _ctx }) => {
|
|
const client = getSupabaseServerClient();
|
|
const logger = await getLogger();
|
|
const api = createCourseManagementApi(client);
|
|
|
|
logger.info({ name: 'course.markAttendance' }, 'Marking attendance...');
|
|
await api.attendance.mark(
|
|
input.sessionId,
|
|
input.participantId,
|
|
input.present,
|
|
);
|
|
logger.info({ name: 'course.markAttendance' }, 'Attendance marked');
|
|
return { success: true };
|
|
});
|
|
|
|
export const createCategory = authActionClient
|
|
.inputSchema(CreateCategorySchema)
|
|
.action(async ({ parsedInput: input, ctx: _ctx }) => {
|
|
const client = getSupabaseServerClient();
|
|
const logger = await getLogger();
|
|
const api = createCourseManagementApi(client);
|
|
|
|
logger.info({ name: 'course.createCategory' }, 'Creating category...');
|
|
const result = await api.referenceData.createCategory(input);
|
|
logger.info({ name: 'course.createCategory' }, 'Category created');
|
|
return { success: true, data: result };
|
|
});
|
|
|
|
export const createInstructor = authActionClient
|
|
.inputSchema(CreateInstructorSchema)
|
|
.action(async ({ parsedInput: input, ctx: _ctx }) => {
|
|
const client = getSupabaseServerClient();
|
|
const logger = await getLogger();
|
|
const api = createCourseManagementApi(client);
|
|
|
|
logger.info({ name: 'course.createInstructor' }, 'Creating instructor...');
|
|
const result = await api.referenceData.createInstructor(input);
|
|
logger.info({ name: 'course.createInstructor' }, 'Instructor created');
|
|
return { success: true, data: result };
|
|
});
|
|
|
|
export const createLocation = authActionClient
|
|
.inputSchema(CreateLocationSchema)
|
|
.action(async ({ parsedInput: input, ctx: _ctx }) => {
|
|
const client = getSupabaseServerClient();
|
|
const logger = await getLogger();
|
|
const api = createCourseManagementApi(client);
|
|
|
|
logger.info({ name: 'course.createLocation' }, 'Creating location...');
|
|
const result = await api.referenceData.createLocation(input);
|
|
logger.info({ name: 'course.createLocation' }, 'Location created');
|
|
return { success: true, data: result };
|
|
});
|
|
|
|
export const createSession = authActionClient
|
|
.inputSchema(CreateSessionSchema)
|
|
.action(async ({ parsedInput: input, ctx: _ctx }) => {
|
|
const client = getSupabaseServerClient();
|
|
const logger = await getLogger();
|
|
const api = createCourseManagementApi(client);
|
|
|
|
logger.info({ name: 'course.createSession' }, 'Creating session...');
|
|
const result = await api.sessions.create(input);
|
|
logger.info({ name: 'course.createSession' }, 'Session created');
|
|
return { success: true, data: result };
|
|
});
|