@@ -402,47 +402,85 @@ export const updateJob = withServerActionAsyncCatcher<
402
402
) . serialize ( ) ;
403
403
} ) ;
404
404
405
- export const deleteJobById = withServerActionAsyncCatcher <
405
+ export const toggleDeleteJobById = withServerActionAsyncCatcher <
406
406
DeleteJobByIdSchemaType ,
407
407
ServerActionReturnType < deletedJob >
408
408
> ( async ( data ) => {
409
409
const result = deleteJobByIdSchema . parse ( data ) ;
410
410
const { id } = result ;
411
- const deletedJob = await prisma . job . update ( {
411
+
412
+ // Fetch the current job's deleted status
413
+ const job = await prisma . job . findUnique ( {
412
414
where : {
413
415
id : id ,
414
416
} ,
415
- data : {
417
+ select : {
416
418
deleted : true ,
419
+ deletedAt : true ,
420
+ } ,
421
+ } ) ;
422
+
423
+ if ( ! job ) {
424
+ throw new Error ( 'Job not found' ) ;
425
+ }
426
+
427
+ const isNowDeleted = ! job . deleted ;
428
+ const deletedAt = isNowDeleted ? new Date ( ) : null ;
429
+
430
+ const updatedJob = await prisma . job . update ( {
431
+ where : {
432
+ id : id ,
433
+ } ,
434
+ data : {
435
+ deleted : isNowDeleted ,
436
+ deletedAt : deletedAt ,
417
437
} ,
418
438
} ) ;
419
- const deletedJobID = deletedJob . id ;
439
+
440
+ const action = updatedJob . deleted ? 'Deleted' : 'Undeleted' ;
441
+ const deletedJobID = updatedJob . id ;
442
+
420
443
revalidatePath ( '/manage' ) ;
421
- return new SuccessResponse ( 'Job Deleted successfully' , 200 , {
444
+
445
+ return new SuccessResponse ( `Job ${ action } successfully` , 200 , {
422
446
deletedJobID,
423
447
} ) . serialize ( ) ;
424
448
} ) ;
425
449
426
- export const approveJob = withAdminServerAction <
450
+ export const toggleApproveJob = withAdminServerAction <
427
451
ApproveJobSchemaType ,
428
452
ServerActionReturnType < ApprovedJobID >
429
453
> ( async ( session , data ) => {
430
454
const result = ApproveJobSchema . safeParse ( data ) ;
431
455
if ( ! result . success ) {
432
456
throw new Error ( result . error . errors . toLocaleString ( ) ) ;
433
457
}
458
+
434
459
const { id } = result . data ;
460
+
461
+ const job = await prisma . job . findUnique ( {
462
+ where : { id : id } ,
463
+ select : { isVerifiedJob : true } ,
464
+ } ) ;
465
+
466
+ if ( ! job ) {
467
+ throw new Error ( 'Job not found' ) ;
468
+ }
469
+
435
470
await prisma . job . update ( {
436
471
where : {
437
472
id : id ,
438
473
} ,
439
474
data : {
440
- isVerifiedJob : true ,
475
+ isVerifiedJob : ! job . isVerifiedJob ,
441
476
} ,
442
477
} ) ;
478
+
443
479
revalidatePath ( '/manage' ) ;
444
- return new SuccessResponse ( 'Job Approved' , 200 , { jobId : id } ) . serialize ( ) ;
480
+ const message = job . isVerifiedJob ? 'Job Unapproved' : 'Job Approved' ;
481
+ return new SuccessResponse ( message , 200 , { jobId : id } ) . serialize ( ) ;
445
482
} ) ;
483
+
446
484
export async function updateExpiredJobs ( ) {
447
485
const currentDate = new Date ( ) ;
448
486
@@ -458,6 +496,18 @@ export async function updateExpiredJobs() {
458
496
} ,
459
497
} ) ;
460
498
}
499
+ export const deleteOldDeltedJobs = async ( ) => {
500
+ const twoWeeksAgo = new Date ( Date . now ( ) - 14 * 24 * 60 * 60 * 1000 ) ;
501
+
502
+ await prisma . job . deleteMany ( {
503
+ where : {
504
+ deleted : true ,
505
+ deletedAt : {
506
+ lte : twoWeeksAgo ,
507
+ } ,
508
+ } ,
509
+ } ) ;
510
+ } ;
461
511
462
512
export async function toggleBookmarkAction ( userId : string , jobId : string ) {
463
513
try {
0 commit comments