Put src/lib/queries.ts
Browse files- src/lib/queries.ts +92 -0
src/lib/queries.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import "server-only";
|
| 2 |
+
import { and, desc, eq, gte, ilike, sql } from "drizzle-orm";
|
| 3 |
+
import { db } from "@/db";
|
| 4 |
+
import { applications, jobs, profiles } from "@/db/schema";
|
| 5 |
+
import { scoreJob, type Job, type MatchResult, type Profile } from "./match";
|
| 6 |
+
|
| 7 |
+
const DAY = 86400_000;
|
| 8 |
+
|
| 9 |
+
export type JobFilters = {
|
| 10 |
+
q?: string;
|
| 11 |
+
remote?: string;
|
| 12 |
+
type?: string;
|
| 13 |
+
source?: string;
|
| 14 |
+
minMatch?: number;
|
| 15 |
+
posted?: string;
|
| 16 |
+
sort?: string;
|
| 17 |
+
limit?: number;
|
| 18 |
+
};
|
| 19 |
+
|
| 20 |
+
export type JobListItem = Omit<Job, "description"> & { score: number };
|
| 21 |
+
|
| 22 |
+
export async function getProfile(userId: string): Promise<Profile> {
|
| 23 |
+
const [p] = await db.select().from(profiles).where(eq(profiles.userId, userId));
|
| 24 |
+
if (p) return p;
|
| 25 |
+
const [created] = await db.insert(profiles).values({ userId }).returning();
|
| 26 |
+
return created;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
export async function searchJobs(profile: Profile, f: JobFilters) {
|
| 30 |
+
const conds = [];
|
| 31 |
+
const terms = (f.q || "").trim().split(/\s+/).filter(Boolean).slice(0, 4);
|
| 32 |
+
for (const t of terms) {
|
| 33 |
+
const p = `%${t}%`;
|
| 34 |
+
conds.push(
|
| 35 |
+
sql`(${jobs.title} ilike ${p} or ${jobs.company} ilike ${p} or ${jobs.tags}::text ilike ${p} or ${jobs.category} ilike ${p})`,
|
| 36 |
+
);
|
| 37 |
+
}
|
| 38 |
+
if (f.remote === "1") conds.push(eq(jobs.remote, true));
|
| 39 |
+
if (f.type) conds.push(ilike(jobs.jobType, `%${f.type}%`));
|
| 40 |
+
if (f.source) conds.push(eq(jobs.source, f.source));
|
| 41 |
+
const days = Number(f.posted);
|
| 42 |
+
if (days > 0) conds.push(gte(jobs.postedAt, new Date(Date.now() - days * DAY)));
|
| 43 |
+
|
| 44 |
+
const rows = await db
|
| 45 |
+
.select()
|
| 46 |
+
.from(jobs)
|
| 47 |
+
.where(conds.length ? and(...conds) : undefined)
|
| 48 |
+
.orderBy(desc(jobs.postedAt))
|
| 49 |
+
.limit(1500);
|
| 50 |
+
|
| 51 |
+
const seen = new Set<string>();
|
| 52 |
+
const unique = rows.filter((j) => {
|
| 53 |
+
const k = `${j.company.toLowerCase().trim()}|${j.title.toLowerCase().replace(/\s+/g, " ").trim()}`;
|
| 54 |
+
if (seen.has(k)) return false;
|
| 55 |
+
seen.add(k);
|
| 56 |
+
return true;
|
| 57 |
+
});
|
| 58 |
+
let scored = unique.map((j) => ({ j, m: scoreJob(j, profile) }));
|
| 59 |
+
if (f.minMatch) scored = scored.filter((s) => s.m.score >= (f.minMatch as number));
|
| 60 |
+
if (f.sort === "new") scored.sort((a, b) => b.j.postedAt.getTime() - a.j.postedAt.getTime());
|
| 61 |
+
else if (f.sort === "salary")
|
| 62 |
+
scored.sort((a, b) => (b.j.salaryMax ?? b.j.salaryMin ?? 0) - (a.j.salaryMax ?? a.j.salaryMin ?? 0));
|
| 63 |
+
else scored.sort((a, b) => b.m.score - a.m.score || b.j.postedAt.getTime() - a.j.postedAt.getTime());
|
| 64 |
+
|
| 65 |
+
const total = scored.length;
|
| 66 |
+
const items: JobListItem[] = scored.slice(0, f.limit ?? 60).map(({ j, m }) => {
|
| 67 |
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
| 68 |
+
const { description, ...rest } = j;
|
| 69 |
+
return { ...rest, score: m.score };
|
| 70 |
+
});
|
| 71 |
+
return { items, total };
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
export async function getJobDetail(userId: string, profile: Profile, id: number) {
|
| 75 |
+
const [job] = await db.select().from(jobs).where(eq(jobs.id, id));
|
| 76 |
+
if (!job) return null;
|
| 77 |
+
const match: MatchResult = scoreJob(job, profile);
|
| 78 |
+
const [app] = await db
|
| 79 |
+
.select({ id: applications.id, status: applications.status })
|
| 80 |
+
.from(applications)
|
| 81 |
+
.where(and(eq(applications.userId, userId), eq(applications.jobId, id)));
|
| 82 |
+
return { job, match, app: app ?? null };
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
export async function getJobStats() {
|
| 86 |
+
const r = await db.execute(sql`
|
| 87 |
+
select count(*)::int as total,
|
| 88 |
+
count(*) filter (where posted_at > now() - interval '24 hours')::int as today,
|
| 89 |
+
count(*) filter (where remote)::int as remote
|
| 90 |
+
from jobs`);
|
| 91 |
+
return r.rows[0] as { total: number; today: number; remote: number };
|
| 92 |
+
}
|