refactor(assignments): move workflows into use cases
This commit is contained in:
@@ -6,7 +6,7 @@ import type {
|
||||
ItemSummary,
|
||||
ItemWithAssetAndMovementCount,
|
||||
ItemWithAssetCount,
|
||||
} from "@/lib/types"
|
||||
} from "@/types"
|
||||
|
||||
export const ItemService = {
|
||||
findAllItemsCount: async (): Promise<number> => {
|
||||
@@ -86,8 +86,11 @@ export const ItemService = {
|
||||
}) as Promise<Item[]>
|
||||
},
|
||||
|
||||
findByIdWithCategory: async (id: string): Promise<ItemSummary | null> => {
|
||||
return prisma.item.findUnique({
|
||||
findByIdWithCategory: async (
|
||||
id: string,
|
||||
db: Prisma.TransactionClient | typeof prisma = prisma,
|
||||
): Promise<ItemSummary | null> => {
|
||||
return db.item.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
category: { select: { id: true, name: true } },
|
||||
@@ -97,8 +100,9 @@ export const ItemService = {
|
||||
|
||||
findByIdWithAssetCount: async (
|
||||
id: string,
|
||||
db: Prisma.TransactionClient | typeof prisma = prisma,
|
||||
): Promise<ItemWithAssetCount | null> => {
|
||||
return prisma.item.findUnique({
|
||||
return db.item.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
category: { select: { id: true, name: true } },
|
||||
@@ -119,15 +123,21 @@ export const ItemService = {
|
||||
})
|
||||
},
|
||||
|
||||
findByName: async (name: string): Promise<Item | null> => {
|
||||
return prisma.item.findFirst({
|
||||
findByName: async (
|
||||
name: string,
|
||||
db: Prisma.TransactionClient | typeof prisma = prisma,
|
||||
): Promise<Item | null> => {
|
||||
return db.item.findFirst({
|
||||
where: { name },
|
||||
include: { category: true, assets: true, movements: true },
|
||||
}) as Promise<Item | null>
|
||||
},
|
||||
|
||||
findById: async (id: string): Promise<Item | null> => {
|
||||
return prisma.item.findUnique({
|
||||
findById: async (
|
||||
id: string,
|
||||
db: Prisma.TransactionClient | typeof prisma = prisma,
|
||||
): Promise<Item | null> => {
|
||||
return db.item.findUnique({
|
||||
where: { id },
|
||||
include: { category: true, assets: true, movements: true },
|
||||
}) as Promise<Item | null>
|
||||
@@ -148,23 +158,53 @@ export const ItemService = {
|
||||
}) as Promise<Item[]>
|
||||
},
|
||||
|
||||
create: async (data: Prisma.ItemCreateInput): Promise<Item> => {
|
||||
return prisma.item.create({ data })
|
||||
create: async (
|
||||
data: Prisma.ItemCreateInput,
|
||||
db: Prisma.TransactionClient | typeof prisma = prisma,
|
||||
): Promise<Item> => {
|
||||
return db.item.create({ data })
|
||||
},
|
||||
|
||||
update: async (id: string, data: Prisma.ItemUpdateInput): Promise<Item> => {
|
||||
return prisma.item.update({ where: { id }, data })
|
||||
update: async (
|
||||
id: string,
|
||||
data: Prisma.ItemUpdateInput,
|
||||
db: Prisma.TransactionClient | typeof prisma = prisma,
|
||||
): Promise<Item> => {
|
||||
return db.item.update({ where: { id }, data })
|
||||
},
|
||||
|
||||
updateStock: async (id: string, quantity: number): Promise<Item> => {
|
||||
return prisma.item.update({
|
||||
updateStock: async (
|
||||
id: string,
|
||||
quantity: number,
|
||||
db: Prisma.TransactionClient | typeof prisma = prisma,
|
||||
): Promise<Item> => {
|
||||
return db.item.update({
|
||||
where: { id },
|
||||
data: { stock: { increment: quantity } },
|
||||
})
|
||||
},
|
||||
|
||||
delete: async (id: string): Promise<Item> => {
|
||||
return prisma.item.update({
|
||||
decrementStockIfAvailable: async (
|
||||
id: string,
|
||||
quantity: number,
|
||||
db: Prisma.TransactionClient | typeof prisma = prisma,
|
||||
): Promise<boolean> => {
|
||||
const result = await db.item.updateMany({
|
||||
where: {
|
||||
id,
|
||||
stock: { gte: quantity },
|
||||
},
|
||||
data: { stock: { decrement: quantity } },
|
||||
})
|
||||
|
||||
return result.count === 1
|
||||
},
|
||||
|
||||
delete: async (
|
||||
id: string,
|
||||
db: Prisma.TransactionClient | typeof prisma = prisma,
|
||||
): Promise<Item> => {
|
||||
return db.item.update({
|
||||
where: { id },
|
||||
data: { deletedAt: new Date() },
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user