FBLA25/src/routes/account/+page.server.ts
drake 77655c779d
All checks were successful
ci / docker_image (push) Successful in 1m37s
ci / deploy (push) Successful in 16s
import cleanup
2025-03-29 18:13:02 -05:00

44 lines
1.3 KiB
TypeScript

import type { PageServerLoad } from './$types';
import { deleteApplicationWithUser, getUserWithCompanyAndApplications } from '$lib/db/index.server';
import { getUserId } from '$lib/index.server';
import { type Actions, fail } from '@sveltejs/kit';
import fs from 'fs';
import path from 'path';
import { writeFileSync } from 'fs';
export const load: PageServerLoad = async ({ cookies }) => {
const id = getUserId(cookies);
const userData = await getUserWithCompanyAndApplications(id);
const resumeExists = fs.existsSync(path.join(process.cwd(), 'uploads', 'resumes', `${id}.pdf`));
return {
...userData,
resumeExists
};
};
export const actions: Actions = {
deleteApplication: async ({ url, cookies }) => {
const id = parseInt(url.searchParams.get('id')!);
try {
await deleteApplicationWithUser(id, getUserId(cookies));
} catch (err) {
return fail(500, { errorMessage: `Internal Server Error: ${err}` });
}
},
uploadResume: async ({ request, cookies }) => {
const formData = await request.formData();
const file = formData.get('resume') as File;
if (!file) {
fail(400, { message: 'invalid' });
}
writeFileSync(
`uploads/resumes/${getUserId(cookies)}.pdf`,
Buffer.from(await file.arrayBuffer())
);
return { success: true };
}
};