Skip to content

Commit

Permalink
rka-45: new company pull jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
konstrybakov committed Jul 28, 2024
1 parent dfe632f commit ef59a83
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 39 deletions.
13 changes: 8 additions & 5 deletions app/add/components/company/actions/create-company.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use server'
import { pullCompanyJobs } from '@/lib/actions/pull-company-jobs'
import {
type QueryCreateCompanyResult,
queryCreateCompany,
Expand All @@ -9,18 +10,20 @@ import type { ActionResponse } from '@/lib/types/api'

export const actionCreateCompany = async (
company: InsertCompany,
): Promise<ActionResponse<Awaited<QueryCreateCompanyResult>>> => {
): Promise<ActionResponse<Awaited<QueryCreateCompanyResult>[number]>> => {
try {
const result = await queryCreateCompany(company)
const [companyEntry] = await queryCreateCompany(company)

logger.debug(result)
logger.debug({ companyEntry }, 'Company created')

await pullCompanyJobs(companyEntry)

return {
error: false,
data: result,
data: companyEntry,
}
} catch (error) {
logger.error(error)
logger.error(error, 'Error while creating company')

const errorMessage =
error instanceof Error
Expand Down
4 changes: 3 additions & 1 deletion app/add/components/company/company-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useAtomValue } from 'jotai'
import { useForm } from 'react-hook-form'
import { z } from 'zod'

import { useRouter } from 'next/navigation'
import { platformAtom, trackerURLAtom } from '../../state'
import { actionCreateCompany } from './actions/create-company'

Expand All @@ -17,6 +18,7 @@ const schema = z.object({
type FormType = z.infer<typeof schema>

export const CompanyName = () => {
const { push } = useRouter()
const platform = useAtomValue(platformAtom)
const trackerURL = useAtomValue(trackerURLAtom)

Expand All @@ -41,7 +43,7 @@ export const CompanyName = () => {
trackerType: 'hiring_platform', // TODO: handle this logic properly
})

console.log(result)
push('/')
}

return (
Expand Down
29 changes: 2 additions & 27 deletions app/api/companies/process/route.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { pullCompanyJobs } from '@/lib/actions/pull-company-jobs'
import { querySelectAllCompanies } from '@/lib/db/queries'
import type { SelectCompany } from '@/lib/db/schema'
import { createPlatform } from '@/lib/hiring-platforms/registry'

import { logger } from '@/lib/logger'
import type { NonNullableProperty } from '@/lib/types/utils'
import { waitFor } from '@/lib/utils/wait-for'
import { ReasonPhrases, StatusCodes } from 'http-status-codes'
import { headers } from 'next/headers'

// TODO: Express this through type also, on a lower level
const isHiringPlatform = (
company: SelectCompany,
): company is NonNullableProperty<SelectCompany, 'hiringPlatform'> =>
company.trackerType === 'hiring_platform'

export const GET = async () => {
const headerList = headers()
const auth = headerList.get('Authorization')
Expand All @@ -35,24 +27,7 @@ export const GET = async () => {
const companies = await querySelectAllCompanies()

for (const company of companies) {
const start = performance.now()

logger.info(`Processing company ${company.name}`)

if (isHiringPlatform(company)) {
const platform = createPlatform(
company.hiringPlatform,
new URL(company.trackerURL),
)

await platform.fetchJobs(company.id)
}

const end = performance.now()

logger.info(
`Company ${company.name} processed in ${(end - start) / 1000}s`,
)
await pullCompanyJobs(company)
}

return Response.json(
Expand Down
7 changes: 5 additions & 2 deletions app/components/list/filter-panel.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
'use client'
import { SegmentedControl } from '@radix-ui/themes'
import { usePathname, useRouter } from 'next/navigation'
import { usePathname, useRouter, useSearchParams } from 'next/navigation'

export const FilterPanel = () => {
const searchParams = useSearchParams()
const pathname = usePathname()
const { push } = useRouter()

const filter = searchParams.get('filter') || 'new'

const handleValueChange = (value: string) => {
const searchParams = new URLSearchParams({ filter: value })

push(`${pathname}?${searchParams}`)
}

return (
<SegmentedControl.Root onValueChange={handleValueChange} defaultValue="new">
<SegmentedControl.Root onValueChange={handleValueChange} value={filter}>
<SegmentedControl.Item value="new">New / Unseen</SegmentedControl.Item>
<SegmentedControl.Item value="topChoice">
Top Choice
Expand Down
29 changes: 29 additions & 0 deletions lib/actions/pull-company-jobs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { SelectCompany } from '../db/schema'
import { createPlatform } from '../hiring-platforms/registry'
import { logger } from '../logger'
import type { NonNullableProperty } from '../types/utils'

// TODO: Express this through type also, on a lower level
const isHiringPlatform = (
company: SelectCompany,
): company is NonNullableProperty<SelectCompany, 'hiringPlatform'> =>
company.trackerType === 'hiring_platform'

export const pullCompanyJobs = async (company: SelectCompany) => {
const start = performance.now()

logger.info(`Processing company ${company.name}`)

if (isHiringPlatform(company)) {
const platform = createPlatform(
company.hiringPlatform,
new URL(company.trackerURL),
)

await platform.fetchJobs(company.id)
}

const end = performance.now()

logger.info(`Company ${company.name} processed in ${(end - start) / 1000}s`)
}
5 changes: 1 addition & 4 deletions lib/db/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { db } from './db'
import { type InsertCompany, type InsertJob, companies, jobs } from './schema'

export const queryCreateCompany = async (company: InsertCompany) => {
const result = await db
.insert(companies)
.values(company)
.returning({ id: companies.id })
const result = await db.insert(companies).values(company).returning()

return result
}
Expand Down

0 comments on commit ef59a83

Please sign in to comment.