44 lines
1.3 KiB
TypeScript
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 };
|
|
}
|
|
};
|