136 lines
3.2 KiB
TypeScript
136 lines
3.2 KiB
TypeScript
import {
|
|
commissionStatusSchema,
|
|
createCommissionInputSchema,
|
|
createCustomerInputSchema,
|
|
createPublicCommissionRequestInputSchema,
|
|
updateCommissionInputSchema,
|
|
updateCommissionStatusInputSchema,
|
|
} from "@cms/content"
|
|
|
|
import { db } from "./client"
|
|
|
|
export const commissionKanbanOrder = commissionStatusSchema.options
|
|
|
|
export async function listCustomers(limit = 200) {
|
|
return db.customer.findMany({
|
|
orderBy: [{ updatedAt: "desc" }],
|
|
take: limit,
|
|
})
|
|
}
|
|
|
|
export async function createCustomer(input: unknown) {
|
|
const payload = createCustomerInputSchema.parse(input)
|
|
|
|
return db.customer.create({
|
|
data: payload,
|
|
})
|
|
}
|
|
|
|
export async function listCommissions(limit = 300) {
|
|
return db.commission.findMany({
|
|
orderBy: [{ updatedAt: "desc" }],
|
|
take: limit,
|
|
include: {
|
|
customer: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
email: true,
|
|
isRecurring: true,
|
|
},
|
|
},
|
|
assignedUser: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
username: true,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
export async function createCommission(input: unknown) {
|
|
const payload = createCommissionInputSchema.parse(input)
|
|
|
|
return db.commission.create({
|
|
data: payload,
|
|
})
|
|
}
|
|
|
|
export async function updateCommission(input: unknown) {
|
|
const payload = updateCommissionInputSchema.parse(input)
|
|
const { id, ...data } = payload
|
|
|
|
return db.commission.update({
|
|
where: { id },
|
|
data,
|
|
})
|
|
}
|
|
|
|
export async function createPublicCommissionRequest(input: unknown) {
|
|
const payload = createPublicCommissionRequestInputSchema.parse(input)
|
|
const normalizedEmail = payload.customerEmail.trim().toLowerCase()
|
|
|
|
return db.$transaction(async (tx) => {
|
|
const existingCustomer = await tx.customer.findFirst({
|
|
where: {
|
|
email: normalizedEmail,
|
|
},
|
|
orderBy: {
|
|
updatedAt: "desc",
|
|
},
|
|
})
|
|
|
|
const customer = existingCustomer
|
|
? await tx.customer.update({
|
|
where: { id: existingCustomer.id },
|
|
data: {
|
|
name: payload.customerName,
|
|
phone: payload.customerPhone ?? existingCustomer.phone,
|
|
instagram: payload.customerInstagram ?? existingCustomer.instagram,
|
|
isRecurring: true,
|
|
},
|
|
})
|
|
: await tx.customer.create({
|
|
data: {
|
|
name: payload.customerName,
|
|
email: normalizedEmail,
|
|
phone: payload.customerPhone,
|
|
instagram: payload.customerInstagram,
|
|
isRecurring: false,
|
|
},
|
|
})
|
|
|
|
return tx.commission.create({
|
|
data: {
|
|
title: payload.title,
|
|
description: payload.description,
|
|
status: "new",
|
|
customerId: customer.id,
|
|
budgetMin: payload.budgetMin,
|
|
budgetMax: payload.budgetMax,
|
|
},
|
|
include: {
|
|
customer: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
email: true,
|
|
isRecurring: true,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
})
|
|
}
|
|
|
|
export async function updateCommissionStatus(input: unknown) {
|
|
const payload = updateCommissionStatusInputSchema.parse(input)
|
|
|
|
return db.commission.update({
|
|
where: { id: payload.id },
|
|
data: { status: payload.status },
|
|
})
|
|
}
|