refractor code,
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { prisma } from '../../lib/prisma'
|
||||
|
||||
export function findMany(companyId: string, filters: { active?: string; public?: string }) {
|
||||
const where: any = { companyId }
|
||||
if (filters.active !== undefined) where.isActive = filters.active === 'true'
|
||||
if (filters.public !== undefined) where.isPublic = filters.public === 'true'
|
||||
return prisma.offer.findMany({ where, orderBy: { createdAt: 'desc' } })
|
||||
}
|
||||
|
||||
export function findByIdOrThrow(id: string, companyId: string) {
|
||||
return prisma.offer.findFirstOrThrow({ where: { id, companyId }, include: { vehicles: true } })
|
||||
}
|
||||
|
||||
export function findFirst(id: string, companyId: string) {
|
||||
return prisma.offer.findFirst({ where: { id, companyId } })
|
||||
}
|
||||
|
||||
export async function create(companyId: string, data: any, vehicleIds: string[]) {
|
||||
return prisma.offer.create({
|
||||
data: {
|
||||
...data,
|
||||
companyId,
|
||||
validFrom: new Date(data.validFrom),
|
||||
validUntil: new Date(data.validUntil),
|
||||
vehicles: vehicleIds.length > 0 ? { create: vehicleIds.map((id) => ({ vehicleId: id })) } : undefined,
|
||||
},
|
||||
include: { vehicles: true },
|
||||
})
|
||||
}
|
||||
|
||||
export async function update(id: string, data: any, vehicleIds?: string[]) {
|
||||
return prisma.$transaction(async (tx) => {
|
||||
await tx.offer.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...data,
|
||||
validFrom: data.validFrom ? new Date(data.validFrom) : undefined,
|
||||
validUntil: data.validUntil ? new Date(data.validUntil) : undefined,
|
||||
},
|
||||
})
|
||||
if (vehicleIds !== undefined) {
|
||||
await tx.offerVehicle.deleteMany({ where: { offerId: id } })
|
||||
if (vehicleIds.length > 0) {
|
||||
await tx.offerVehicle.createMany({ data: vehicleIds.map((vehicleId) => ({ offerId: id, vehicleId })) })
|
||||
}
|
||||
}
|
||||
return tx.offer.findUniqueOrThrow({ where: { id }, include: { vehicles: true } })
|
||||
})
|
||||
}
|
||||
|
||||
export function deleteMany(id: string, companyId: string) {
|
||||
return prisma.offer.deleteMany({ where: { id, companyId } })
|
||||
}
|
||||
|
||||
export function setActive(id: string, companyId: string, isActive: boolean) {
|
||||
return prisma.offer.updateMany({ where: { id, companyId }, data: { isActive } })
|
||||
}
|
||||
|
||||
export async function getStats(id: string, companyId: string) {
|
||||
const offer = await prisma.offer.findFirstOrThrow({ where: { id, companyId } })
|
||||
const reservations = await prisma.reservation.count({ where: { offerId: offer.id } })
|
||||
return { redemptions: offer.redemptionCount, reservations }
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import { Router } from 'express'
|
||||
import { requireCompanyAuth } from '../../middleware/requireCompanyAuth'
|
||||
import { requireTenant } from '../../middleware/requireTenant'
|
||||
import { requireSubscription } from '../../middleware/requireSubscription'
|
||||
import { requireRole } from '../../middleware/requireRole'
|
||||
import { parseBody, parseQuery, parseParams } from '../../http/validate'
|
||||
import { ok, created } from '../../http/respond'
|
||||
import * as service from './offer.service'
|
||||
import { offerSchema, listQuerySchema, idParamSchema } from './offer.schemas'
|
||||
|
||||
const router = Router()
|
||||
|
||||
router.use(requireCompanyAuth, requireTenant, requireSubscription)
|
||||
|
||||
router.get('/', async (req, res, next) => {
|
||||
try {
|
||||
const query = parseQuery(listQuerySchema, req)
|
||||
ok(res, { data: await service.listOffers(req.companyId, query) })
|
||||
} catch (err) { next(err) }
|
||||
})
|
||||
|
||||
router.post('/', requireRole('MANAGER'), async (req, res, next) => {
|
||||
try {
|
||||
const { vehicleIds, ...body } = parseBody(offerSchema, req)
|
||||
created(res, { data: await service.createOffer(req.companyId, body, vehicleIds) })
|
||||
} catch (err) { next(err) }
|
||||
})
|
||||
|
||||
router.get('/:id', async (req, res, next) => {
|
||||
try {
|
||||
const { id } = parseParams(idParamSchema, req)
|
||||
ok(res, { data: await service.getOffer(id, req.companyId) })
|
||||
} catch (err) { next(err) }
|
||||
})
|
||||
|
||||
router.patch('/:id', requireRole('MANAGER'), async (req, res, next) => {
|
||||
try {
|
||||
const { id } = parseParams(idParamSchema, req)
|
||||
const { vehicleIds, ...body } = parseBody(offerSchema.partial(), req)
|
||||
ok(res, { data: await service.updateOffer(id, req.companyId, body, vehicleIds) })
|
||||
} catch (err) { next(err) }
|
||||
})
|
||||
|
||||
router.delete('/:id', requireRole('MANAGER'), async (req, res, next) => {
|
||||
try {
|
||||
const { id } = parseParams(idParamSchema, req)
|
||||
await service.deleteOffer(id, req.companyId)
|
||||
ok(res, { data: { success: true } })
|
||||
} catch (err) { next(err) }
|
||||
})
|
||||
|
||||
router.post('/:id/activate', requireRole('MANAGER'), async (req, res, next) => {
|
||||
try {
|
||||
const { id } = parseParams(idParamSchema, req)
|
||||
await service.setOfferActive(id, req.companyId, true)
|
||||
ok(res, { data: { success: true } })
|
||||
} catch (err) { next(err) }
|
||||
})
|
||||
|
||||
router.post('/:id/deactivate', requireRole('MANAGER'), async (req, res, next) => {
|
||||
try {
|
||||
const { id } = parseParams(idParamSchema, req)
|
||||
await service.setOfferActive(id, req.companyId, false)
|
||||
ok(res, { data: { success: true } })
|
||||
} catch (err) { next(err) }
|
||||
})
|
||||
|
||||
router.get('/:id/stats', async (req, res, next) => {
|
||||
try {
|
||||
const { id } = parseParams(idParamSchema, req)
|
||||
ok(res, { data: await service.getOfferStats(id, req.companyId) })
|
||||
} catch (err) { next(err) }
|
||||
})
|
||||
|
||||
export default router
|
||||
@@ -0,0 +1,31 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
export const offerSchema = z.object({
|
||||
title: z.string().min(1),
|
||||
description: z.string().optional(),
|
||||
termsAndConds: z.string().optional(),
|
||||
type: z.enum(['PERCENTAGE', 'FIXED_AMOUNT', 'FREE_DAY', 'SPECIAL_RATE']),
|
||||
discountValue: z.number().int().min(0),
|
||||
specialRate: z.number().int().optional(),
|
||||
appliesToAll: z.boolean().default(true),
|
||||
categories: z.array(z.enum(['ECONOMY', 'COMPACT', 'MIDSIZE', 'FULLSIZE', 'SUV', 'LUXURY', 'VAN', 'TRUCK'])).default([]),
|
||||
minRentalDays: z.number().int().optional(),
|
||||
maxRentalDays: z.number().int().optional(),
|
||||
promoCode: z.string().optional(),
|
||||
maxRedemptions: z.number().int().optional(),
|
||||
validFrom: z.string().datetime(),
|
||||
validUntil: z.string().datetime(),
|
||||
isActive: z.boolean().default(true),
|
||||
isPublic: z.boolean().default(true),
|
||||
isFeatured: z.boolean().default(false),
|
||||
vehicleIds: z.array(z.string()).default([]),
|
||||
})
|
||||
|
||||
export const listQuerySchema = z.object({
|
||||
active: z.string().optional(),
|
||||
public: z.string().optional(),
|
||||
})
|
||||
|
||||
export const idParamSchema = z.object({
|
||||
id: z.string().min(1),
|
||||
})
|
||||
@@ -0,0 +1,32 @@
|
||||
import { NotFoundError } from '../../http/errors'
|
||||
import * as repo from './offer.repo'
|
||||
|
||||
export function listOffers(companyId: string, filters: { active?: string; public?: string }) {
|
||||
return repo.findMany(companyId, filters)
|
||||
}
|
||||
|
||||
export function getOffer(id: string, companyId: string) {
|
||||
return repo.findByIdOrThrow(id, companyId)
|
||||
}
|
||||
|
||||
export function createOffer(companyId: string, data: any, vehicleIds: string[]) {
|
||||
return repo.create(companyId, data, vehicleIds)
|
||||
}
|
||||
|
||||
export async function updateOffer(id: string, companyId: string, data: any, vehicleIds?: string[]) {
|
||||
const existing = await repo.findFirst(id, companyId)
|
||||
if (!existing) throw new NotFoundError('Offer not found')
|
||||
return repo.update(id, data, vehicleIds)
|
||||
}
|
||||
|
||||
export function deleteOffer(id: string, companyId: string) {
|
||||
return repo.deleteMany(id, companyId)
|
||||
}
|
||||
|
||||
export function setOfferActive(id: string, companyId: string, isActive: boolean) {
|
||||
return repo.setActive(id, companyId, isActive)
|
||||
}
|
||||
|
||||
export function getOfferStats(id: string, companyId: string) {
|
||||
return repo.getStats(id, companyId)
|
||||
}
|
||||
Reference in New Issue
Block a user