[resumes][feat] delete resume file on deletion (#555)

pull/556/head
Keane Chan 2 years ago committed by GitHub
parent 0e02e23015
commit 3e91ff2d6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -154,7 +154,6 @@ model ResumesResume {
id String @id @default(cuid()) id String @id @default(cuid())
userId String userId String
title String @db.Text title String @db.Text
// TODO: Update role, experience, location to use Enums
role String @db.Text role String @db.Text
experience String @db.Text experience String @db.Text
locationId String locationId String

@ -20,7 +20,6 @@ export type BadgeInfo = {
title: string; title: string;
}; };
// TODO: Add other badges in
export type BadgePayload = { export type BadgePayload = {
maxResumeUpvoteCount: number; maxResumeUpvoteCount: number;
reviewedResumesCount: number; reviewedResumesCount: number;

@ -17,8 +17,8 @@ export default async function handler(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse, res: NextApiResponse,
) { ) {
if (req.method === 'POST') { try {
try { if (req.method === 'POST') {
const form = formidable({ keepExtensions: true }); const form = formidable({ keepExtensions: true });
form.parse(req, async (err, fields, files) => { form.parse(req, async (err, fields, files) => {
if (err) { if (err) {
@ -45,8 +45,26 @@ export default async function handler(
url: `${BASE_FILE_URL}/${key}/${filePath}`, url: `${BASE_FILE_URL}/${key}/${filePath}`,
}); });
}); });
} catch (error: unknown) { } else if (req.method === 'DELETE') {
return Promise.reject(error); const { key, fileUrl } = req.query;
const storageKey = key as string;
const url = fileUrl as string;
const filePath = url.substring(url.lastIndexOf('/') + 1);
const { error } = await supabase.storage
.from(storageKey)
.remove([filePath]);
if (error) {
throw error;
}
return res.status(200).json({
message: `File ${filePath} has been deleted`,
});
} }
} catch (error: unknown) {
return Promise.reject(error);
} }
} }

@ -1,3 +1,4 @@
import axios from 'axios';
import clsx from 'clsx'; import clsx from 'clsx';
import formatDistanceToNow from 'date-fns/formatDistanceToNow'; import formatDistanceToNow from 'date-fns/formatDistanceToNow';
import Error from 'next/error'; import Error from 'next/error';
@ -25,6 +26,7 @@ import ResumePdf from '~/components/resumes/ResumePdf';
import ResumeExpandableText from '~/components/resumes/shared/ResumeExpandableText'; import ResumeExpandableText from '~/components/resumes/shared/ResumeExpandableText';
import loginPageHref from '~/components/shared/loginPageHref'; import loginPageHref from '~/components/shared/loginPageHref';
import { RESUME_STORAGE_KEY } from '~/constants/file-storage-keys';
import { import {
BROWSE_TABS_VALUES, BROWSE_TABS_VALUES,
getFilterLabel, getFilterLabel,
@ -183,10 +185,15 @@ export default function ResumeReviewPage() {
return deleteResumeMutation.mutate( return deleteResumeMutation.mutate(
{ id: resumeId as string }, { id: resumeId as string },
{ {
onSuccess() { async onSuccess() {
// TODO: Delete from file storage // Delete from file storage
if (detailsQuery.data != null) {
await axios.delete(
`/api/file-storage?key=${RESUME_STORAGE_KEY}&fileUrl=${detailsQuery.data.url}`,
);
}
// redirect to browse with default settings // Redirect to browse with default settings
router.push({ router.push({
pathname: '/resumes', pathname: '/resumes',
query: { query: {

Loading…
Cancel
Save