50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import { db } from "@pkg/db";
|
|
import { CouponRepository } from "./repository";
|
|
import type { CreateCouponPayload, UpdateCouponPayload } from "./data";
|
|
import type { UserModel } from "@pkg/logic/domains/user/data/entities";
|
|
|
|
export class CouponUseCases {
|
|
private repo: CouponRepository;
|
|
|
|
constructor(repo: CouponRepository) {
|
|
this.repo = repo;
|
|
}
|
|
|
|
async getAllCoupons() {
|
|
return this.repo.getAllCoupons();
|
|
}
|
|
|
|
async getCouponById(id: number) {
|
|
return this.repo.getCouponById(id);
|
|
}
|
|
|
|
async createCoupon(currentUser: UserModel, payload: CreateCouponPayload) {
|
|
// Set the current user as the creator
|
|
const payloadWithUser = {
|
|
...payload,
|
|
createdBy: currentUser.id,
|
|
};
|
|
return this.repo.createCoupon(payloadWithUser);
|
|
}
|
|
|
|
async updateCoupon(payload: UpdateCouponPayload) {
|
|
return this.repo.updateCoupon(payload);
|
|
}
|
|
|
|
async deleteCoupon(id: number) {
|
|
return this.repo.deleteCoupon(id);
|
|
}
|
|
|
|
async toggleCouponStatus(id: number, isActive: boolean) {
|
|
return this.repo.toggleCouponStatus(id, isActive);
|
|
}
|
|
|
|
async getActiveCoupons() {
|
|
return this.repo.getActiveCoupons();
|
|
}
|
|
}
|
|
|
|
export function getCouponUseCases() {
|
|
return new CouponUseCases(new CouponRepository(db));
|
|
}
|