Skip to content

Commit

Permalink
new stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmegood committed Feb 15, 2024
1 parent 8766857 commit 563de12
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 150 deletions.
8 changes: 4 additions & 4 deletions app/routes/c.$companyId.accounts+/__account-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { requireCompanyUserWithRBAC } from '#app/utils/permissions.server'
// TODO: Make a better component to handle country, state, and city inputs
// ? Maybe a select component with api data

const CompanyAccountsNewSchema = z.object({
const CompanyAccountsEditorSchema = z.object({
id: z.string().optional(),
name: z.string().min(3).max(40),
uniqueId: z.string().min(4).max(24).optional(),
Expand Down Expand Up @@ -48,7 +48,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
const formData = await request.formData()

const submission = await parseWithZod(formData, {
schema: CompanyAccountsNewSchema.superRefine(async (data, ctx) => {
schema: CompanyAccountsEditorSchema.superRefine(async (data, ctx) => {
const checkName = await prisma.company.findFirst({
where: {
accounts: {
Expand Down Expand Up @@ -147,10 +147,10 @@ export function AccountEditor({

const [form, fields] = useForm({
id: 'company-accounts-editor-form',
constraint: getZodConstraint(CompanyAccountsNewSchema),
constraint: getZodConstraint(CompanyAccountsEditorSchema),
lastResult: actionData?.result,
onValidate({ formData }) {
return parseWithZod(formData, { schema: CompanyAccountsNewSchema })
return parseWithZod(formData, { schema: CompanyAccountsEditorSchema })
},
defaultValue: {
...account,
Expand Down
27 changes: 12 additions & 15 deletions app/routes/c.$companyId.sales+/__sale-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getFormProps, getInputProps, useForm } from '@conform-to/react'
import { getZodConstraint, parseWithZod } from '@conform-to/zod'
import { Account, type SaleInvoice } from '@prisma/client'
import { type Account, type SaleInvoice } from '@prisma/client'
import {
json,
type ActionFunctionArgs,
Expand All @@ -17,25 +17,23 @@ import { requireCompanyUserWithRBAC } from '#app/utils/permissions.server'
// TODO: Make a better component to handle country, state, and city inputs
// ? Maybe a select component with api data

const CompanyAccountsNewSchema = z.object({
const CompanySaleEditorSchema = z.object({
id: z.string().optional(),
invoiceNumber: z.string().min(4).max(24).optional(),
dueDate: z.string().min(3).max(40),
dateIssued: z.string().email().optional(),
issuedToId: z.string().min(7).max(15).optional(),
address: z.string().min(4).max(40).optional(),
country: z.string().min(4).max(24).optional(),
city: z.string().min(4).max(24).optional(),
state: z.string().min(4).max(24).optional(),
zip: z.string().min(4).max(12).optional(),
// description: z.string().min(4).max(80).optional(),
issuedById: z.string().min(4).max(40).optional(),
totalAmount: z.string().min(4).max(24).optional(),
transactionStatusKey: z.string().min(4).max(24).optional(),
description: z.string().min(4).max(80).optional(),
})

export async function action({ request, params }: ActionFunctionArgs) {
const user = await requireCompanyUserWithRBAC({
request,
companyId: params.companyId!,
permission: 'create:company-account',
permission: 'create:company-sale',
select: {
userCompanies: {
select: {
Expand All @@ -48,7 +46,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
const formData = await request.formData()

const submission = await parseWithZod(formData, {
schema: CompanyAccountsNewSchema.superRefine(async (data, ctx) => {
schema: CompanySaleEditorSchema.superRefine(async (data, ctx) => {
const checkName = await prisma.company.findFirst({
where: {
accounts: {
Expand Down Expand Up @@ -139,20 +137,19 @@ export function SaleEditor({
| 'createdAt'
| 'updatedAt'
| 'transactionStatusKey'
> & {
issuedTo: Pick<Account, 'name' | 'uniqueId'>
}
| 'description'
> & { issuedTo: Pick<Account, 'name' | 'uniqueId'> }
>
}) {
const actionData = useActionData<typeof action>()
const isPending = useIsPending()

const [form, fields] = useForm({
id: 'company-sale-editor-form',
constraint: getZodConstraint(CompanyAccountsNewSchema),
constraint: getZodConstraint(CompanySaleEditorSchema),
lastResult: actionData?.result,
onValidate({ formData }) {
return parseWithZod(formData, { schema: CompanyAccountsNewSchema })
return parseWithZod(formData, { schema: CompanySaleEditorSchema })
},
defaultValue: {
...sale,
Expand Down
20 changes: 17 additions & 3 deletions app/routes/c.$companyId.sales+/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { type LoaderFunctionArgs, json } from '@remix-run/node'
import { useLoaderData } from '@remix-run/react'
import { requireUserId } from '#app/utils/auth.server'
import { PLATFORM_STATUS } from '#app/utils/constants/platform-status'
import { TRANSACTION_STATUS } from '#app/utils/constants/transaction-status'
import { prisma } from '#app/utils/db.server'

export async function loader({ request, params }: LoaderFunctionArgs) {
const userId = await requireUserId(request)

const company = await prisma.company.findUnique({
where: {
id: params.companyId,
Expand All @@ -19,6 +19,8 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
platformStatusKey: PLATFORM_STATUS.ACTIVE.KEY,
},
select: {
id: true,
invoiceCount: true,
saleInvoices: {
select: {
id: true,
Expand All @@ -31,10 +33,16 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
totalAmount: true,
},
},
accounts: {
select: {
id: true,
name: true,
},
},
},
})

invariantResponse(company, 'Not found', { status: 404 })
// return json({ company })

return json({ company })
}
Expand All @@ -45,7 +53,13 @@ export default function CompanySalesOverview() {
return (
<div className="">
<h1>Overview Route</h1>
{/* <div>{data.company}</div> */}
<div>
{data.company.saleInvoices.map(invoice => (
<div key={invoice.id}>
{invoice.id} - {invoice.issuedTo.name} - {invoice.totalAmount}
</div>
))}
</div>
</div>
)
}
30 changes: 30 additions & 0 deletions app/routes/c.$companyId.sales+/new.$accountId.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { type LoaderFunctionArgs } from '@remix-run/node'
import { prisma } from '#app/utils/db.server'
import { requireCompanyUserWithRBAC } from '#app/utils/permissions.server'
import { SaleEditor, action } from './__sale-editor'

export async function loader({ request, params }: LoaderFunctionArgs) {
await requireCompanyUserWithRBAC({
request,
companyId: params.companyId!,
permission: 'create:company-account',
})

const account = await prisma.account.findFirst({
where: {
id: params.accountId,
companyId: params.companyId,
},
select: {},
})

return null
}

export { action }

export default function CompanyAccountsEdit() {
const data = useLoaderData<typeof loader>()

return <SaleEditor sale={data.account} />
}
16 changes: 16 additions & 0 deletions app/routes/c.$companyId.sales+/new.guest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { type LoaderFunctionArgs } from '@remix-run/node'
import { requireCompanyUserWithRBAC } from '#app/utils/permissions.server'
import { SaleEditor, action } from './__sale-editor'

export async function loader({ request, params }: LoaderFunctionArgs) {
await requireCompanyUserWithRBAC({
request,
companyId: params.companyId!,
permission: 'create:company-account',
})

return null
}

export { action }
export default SaleEditor
42 changes: 29 additions & 13 deletions app/utils/cache.server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import fs from 'fs'
import {
cachified as baseCachified,
lruCacheAdapter,
verboseReporter,
mergeReporters,
type CacheEntry,
type Cache as CachifiedCache,
type CachifiedOptions,
type Cache,
totalTtl,
type CreateReporter,
} from '@epic-web/cachified'
import { remember } from '@epic-web/remember'
import Database from 'better-sqlite3'
Expand Down Expand Up @@ -52,7 +54,19 @@ const lru = remember(
() => new LRUCache<string, CacheEntry<unknown>>({ max: 5000 }),
)

export const lruCache = lruCacheAdapter(lru)
export const lruCache = {
name: 'app-memory-cache',
set: (key, value) => {
const ttl = totalTtl(value?.metadata)
lru.set(key, value, {
ttl: ttl === Infinity ? undefined : ttl,
start: value?.metadata?.createdTime,
})
return value
},
get: key => lru.get(key),
delete: key => lru.delete(key),
} satisfies Cache

const cacheEntrySchema = z.object({
metadata: z.object({
Expand Down Expand Up @@ -153,15 +167,17 @@ export async function searchCacheKeys(search: string, limit: number) {
}
}

export async function cachified<Value>({
timings,
reporter = verboseReporter(),
...options
}: CachifiedOptions<Value> & {
timings?: Timings
}): Promise<Value> {
return baseCachified({
...options,
reporter: mergeReporters(cachifiedTimingReporter(timings), reporter),
})
export async function cachified<Value>(
{
timings,
...options
}: CachifiedOptions<Value> & {
timings?: Timings
},
reporter: CreateReporter<Value> = verboseReporter<Value>(),
): Promise<Value> {
return baseCachified(
options,
mergeReporters(cachifiedTimingReporter(timings), reporter),
)
}
Loading

0 comments on commit 563de12

Please sign in to comment.