Skip to content

Commit

Permalink
feat(postings): ability to expire postings (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
amandesai01 authored Oct 21, 2024
1 parent e4d1512 commit 1166920
Show file tree
Hide file tree
Showing 14 changed files with 493 additions and 11 deletions.
8 changes: 7 additions & 1 deletion app/components/admin/PostingCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ if (props.posting && props.posting.tagsCSV) {
<footer class="mt-5">
<div class="text-sm font-medium text-gray-500 mb-2">Created {{ timeAgo(new Date(posting.createdAt)) }}</div>
<div class="flex justify-between items-center">
<div
class="text-xs font-medium rounded-lg text-center px-2.5 py-1 bg-red-100 text-red-700"
v-if="posting.isExpired"
>
Expired
</div>
<div
class="text-xs font-medium rounded-lg text-center px-2.5 py-1 bg-green-100 text-green-700"
v-if="posting.isPublished"
v-else-if="posting.isPublished"
>
Published
</div>
Expand Down
6 changes: 6 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const composeVersion = () => {
}
};

const cronSchedule: Record<string, string[]> = {
// Every day, at 00:00.
'0 0 * * *': ['cron:expire-postings'],
};

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
future: {
Expand All @@ -17,6 +22,7 @@ export default defineNuxtConfig({
experimental: {
tasks: true,
},
scheduledTasks: cronSchedule,
},
routeRules: {
'/admin/**': { ssr: false },
Expand Down
27 changes: 22 additions & 5 deletions server/api/posting/index.put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ export default defineEventHandler(async (event) => {
console.log('updating posting id', q.id);
}

const isPostingAlreadyExpired = (
await database
.select({ isExpired: jobPostingsTable.isExpired })
.from(jobPostingsTable)
.where(eq(jobPostingsTable.id, q.id))
)[0]?.isExpired;

if (isPostingAlreadyExpired) {
throw createError({
statusCode: 400,
statusMessage: 'Expired postings cannot be updated.',
});
}

const updateQuery = {
...q,
updatedAt: new Date(),
Expand All @@ -23,12 +37,15 @@ export default defineEventHandler(async (event) => {
)[0] as JobPosting;

const postings = (await general_memoryStorage.getItem<JobPosting[]>('postings')) || [];
const updatedPostings = postings.map((p) => {
if (p.id == updatedJobPosting.id) return updatedJobPosting;
return p;
});

await general_memoryStorage.setItem('postings', updatedPostings);

await general_memoryStorage.setItem(
'postings',
postings.map((p) => {
if (p.id == updatedJobPosting.id) return updatedJobPosting;
return p;
})
'totalActivePostings',
updatedPostings.filter((p) => !p.isExpired && p.isPublished).length
);
});
2 changes: 1 addition & 1 deletion server/api/public/posting.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default defineEventHandler(async (event) => {
}

const postings = ((await general_memoryStorage.getItem<JobPosting[]>('postings')) || [])
.filter((p) => p.id === query.id && p.isPublished)
.filter((p) => p.id === query.id && p.isPublished && !p.isExpired)
.map((p) => {
return {
...p,
Expand Down
2 changes: 1 addition & 1 deletion server/api/public/postings.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type JobPosting } from '~~/server/db/schema';

export default defineEventHandler(async (_) => {
const postings = ((await general_memoryStorage.getItem<JobPosting[]>('postings')) || [])
.filter((p) => p.isPublished)
.filter((p) => p.isPublished && !p.isExpired)
.map((p) => ({
...p,
contents: null,
Expand Down
2 changes: 2 additions & 0 deletions server/db/migrations/0011_clever_spiral.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "job_postings" ADD COLUMN "is_expired" boolean DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE "job_postings" ADD COLUMN "valid_till" date;
Loading

0 comments on commit 1166920

Please sign in to comment.