enh
This commit is contained in:
parent
588cbe00f3
commit
1b62c6add4
5 changed files with 10 additions and 66 deletions
|
|
@ -73,7 +73,6 @@ export {
|
||||||
updateSkuDeals,
|
updateSkuDeals,
|
||||||
replaceProductTags,
|
replaceProductTags,
|
||||||
mergeSkus,
|
mergeSkus,
|
||||||
toggleSkuOutOfStock,
|
|
||||||
updateSlotProducts,
|
updateSlotProducts,
|
||||||
getSlotProductIds,
|
getSlotProductIds,
|
||||||
getSlotsProductIds,
|
getSlotsProductIds,
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import {
|
||||||
getAllProducts as getAllProductsInDb,
|
getAllProducts as getAllProductsInDb,
|
||||||
getProductById as getProductByIdInDb,
|
getProductById as getProductByIdInDb,
|
||||||
deleteProduct as deleteProductInDb,
|
deleteProduct as deleteProductInDb,
|
||||||
toggleProductOutOfStock as toggleProductOutOfStockInDb,
|
|
||||||
updateSlotProducts as updateSlotProductsInDb,
|
updateSlotProducts as updateSlotProductsInDb,
|
||||||
getSlotProductIds as getSlotProductIdsInDb,
|
getSlotProductIds as getSlotProductIdsInDb,
|
||||||
getSlotsProductIds as getSlotsProductIdsInDb,
|
getSlotsProductIds as getSlotsProductIdsInDb,
|
||||||
|
|
@ -21,11 +20,9 @@ import {
|
||||||
checkProductExistsByName,
|
checkProductExistsByName,
|
||||||
checkUnitExists,
|
checkUnitExists,
|
||||||
createProduct as createProductInDb,
|
createProduct as createProductInDb,
|
||||||
createSpecialDealsForProduct,
|
|
||||||
replaceProductTags,
|
replaceProductTags,
|
||||||
getProductImagesById,
|
getProductImagesById,
|
||||||
updateProduct as updateProductInDb,
|
updateProduct as updateProductInDb,
|
||||||
updateProductDeals,
|
|
||||||
checkProductTagExistsByName,
|
checkProductTagExistsByName,
|
||||||
createProductTag as createProductTagInDb,
|
createProductTag as createProductTagInDb,
|
||||||
updateProductTag as updateProductTagInDb,
|
updateProductTag as updateProductTagInDb,
|
||||||
|
|
@ -44,7 +41,6 @@ import type {
|
||||||
AdminProductListResponse,
|
AdminProductListResponse,
|
||||||
AdminProductResponse,
|
AdminProductResponse,
|
||||||
AdminDeleteProductResult,
|
AdminDeleteProductResult,
|
||||||
AdminToggleOutOfStockResult,
|
|
||||||
AdminUpdateSlotProductsResult,
|
AdminUpdateSlotProductsResult,
|
||||||
AdminSlotProductIdsResult,
|
AdminSlotProductIdsResult,
|
||||||
AdminSlotsProductIdsResult,
|
AdminSlotsProductIdsResult,
|
||||||
|
|
@ -187,46 +183,6 @@ export const productRouter = router({
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
||||||
toggleOutOfStock: protectedProcedure
|
|
||||||
.input(z.object({
|
|
||||||
id: z.number(),
|
|
||||||
}))
|
|
||||||
.mutation(async ({ input }): Promise<AdminToggleOutOfStockResult> => {
|
|
||||||
const { id } = input;
|
|
||||||
|
|
||||||
const updatedProduct = await toggleProductOutOfStockInDb(id)
|
|
||||||
|
|
||||||
/*
|
|
||||||
// Old implementation - direct DB queries:
|
|
||||||
const product = await db.query.productInfo.findFirst({
|
|
||||||
where: eq(productInfo.id, id),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!product) {
|
|
||||||
throw new ApiError("Product not found", 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [updatedProduct] = await db
|
|
||||||
.update(productInfo)
|
|
||||||
.set({
|
|
||||||
isOutOfStock: !product.isOutOfStock,
|
|
||||||
})
|
|
||||||
.where(eq(productInfo.id, id))
|
|
||||||
.returning();
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (!updatedProduct) {
|
|
||||||
throw new ApiError('Product not found', 404)
|
|
||||||
}
|
|
||||||
|
|
||||||
await scheduleStoreInitialization()
|
|
||||||
|
|
||||||
return {
|
|
||||||
product: updatedProduct,
|
|
||||||
message: `Product marked as ${updatedProduct.isOutOfStock ? 'out of stock' : 'in stock'}`,
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
|
|
||||||
createProduct: protectedProcedure
|
createProduct: protectedProcedure
|
||||||
.input(z.object({
|
.input(z.object({
|
||||||
name: z.string().min(1, 'Name is required'),
|
name: z.string().min(1, 'Name is required'),
|
||||||
|
|
@ -386,16 +342,16 @@ export const productRouter = router({
|
||||||
updateSlotProducts: protectedProcedure
|
updateSlotProducts: protectedProcedure
|
||||||
.input(z.object({
|
.input(z.object({
|
||||||
slotId: z.string(),
|
slotId: z.string(),
|
||||||
productIds: z.array(z.string()),
|
skuIds: z.array(z.string()),
|
||||||
}))
|
}))
|
||||||
.mutation(async ({ input }): Promise<AdminUpdateSlotProductsResult> => {
|
.mutation(async ({ input }): Promise<AdminUpdateSlotProductsResult> => {
|
||||||
const { slotId, productIds } = input;
|
const { slotId, skuIds } = input;
|
||||||
|
|
||||||
if (!Array.isArray(productIds)) {
|
if (!Array.isArray(skuIds)) {
|
||||||
throw new ApiError("productIds must be an array", 400);
|
throw new ApiError("skuIds must be an array", 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await updateSlotProductsInDb(slotId, productIds)
|
const result = await updateSlotProductsInDb(slotId, skuIds)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// Old implementation - direct DB queries:
|
// Old implementation - direct DB queries:
|
||||||
|
|
@ -460,7 +416,7 @@ export const productRouter = router({
|
||||||
.query(async ({ input }): Promise<AdminSlotProductIdsResult> => {
|
.query(async ({ input }): Promise<AdminSlotProductIdsResult> => {
|
||||||
const { slotId } = input;
|
const { slotId } = input;
|
||||||
|
|
||||||
const productIds = await getSlotProductIdsInDb(slotId)
|
const skuIds = await getSlotProductIdsInDb(slotId)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// Old implementation - direct DB queries:
|
// Old implementation - direct DB queries:
|
||||||
|
|
@ -474,12 +430,7 @@ export const productRouter = router({
|
||||||
const productIds = associations.map(assoc => assoc.productId);
|
const productIds = associations.map(assoc => assoc.productId);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
productIds,
|
skuIds,
|
||||||
};
|
|
||||||
*/
|
|
||||||
|
|
||||||
return {
|
|
||||||
productIds,
|
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,6 @@ export {
|
||||||
updateSkuDeals,
|
updateSkuDeals,
|
||||||
replaceProductTags,
|
replaceProductTags,
|
||||||
mergeSkus,
|
mergeSkus,
|
||||||
toggleSkuOutOfStock,
|
|
||||||
updateSlotProducts,
|
updateSlotProducts,
|
||||||
getSlotProductIds,
|
getSlotProductIds,
|
||||||
getSlotsProductIds,
|
getSlotsProductIds,
|
||||||
|
|
|
||||||
|
|
@ -370,7 +370,6 @@ export async function updateProduct(id: number, input: any): Promise<AdminProduc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function toggleSkuOutOfStock(id: number): Promise<AdminSku | null> {
|
|
||||||
const product = await db.query.productSkus.findFirst({
|
const product = await db.query.productSkus.findFirst({
|
||||||
where: eq(productSkus.id, id),
|
where: eq(productSkus.id, id),
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -466,7 +466,8 @@ export interface AdminSpecialDeal {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AdminProductWithDetails extends AdminProduct {
|
export interface AdminProductWithDetails extends AdminProduct {
|
||||||
unit: AdminUnit;
|
store: Store | null;
|
||||||
|
skus: AdminSku[];
|
||||||
deals: AdminSpecialDeal[];
|
deals: AdminSpecialDeal[];
|
||||||
tags: AdminProductTagInfo[];
|
tags: AdminProductTagInfo[];
|
||||||
}
|
}
|
||||||
|
|
@ -484,11 +485,6 @@ export interface AdminDeleteProductResult {
|
||||||
message: string;
|
message: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AdminToggleOutOfStockResult {
|
|
||||||
product: AdminProduct;
|
|
||||||
message: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface AdminUpdateSlotProductsResult {
|
export interface AdminUpdateSlotProductsResult {
|
||||||
message: string;
|
message: string;
|
||||||
added: number;
|
added: number;
|
||||||
|
|
@ -496,7 +492,7 @@ export interface AdminUpdateSlotProductsResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AdminSlotProductIdsResult {
|
export interface AdminSlotProductIdsResult {
|
||||||
productIds: number[];
|
skuIds: number[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AdminSlotsProductIdsResult = Record<number, number[]>;
|
export type AdminSlotsProductIdsResult = Record<number, number[]>;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue