item create error fixed
This commit is contained in:
parent
71bd4e8354
commit
28d38bc695
@ -8,6 +8,13 @@ const ITEM_SERIES_CODE = 'ITEM';
|
|||||||
|
|
||||||
const SEARCH_FIELDS = ['item_code', 'item_name'];
|
const SEARCH_FIELDS = ['item_code', 'item_name'];
|
||||||
|
|
||||||
|
const SOFT_DELETE_TABLES = new Set([
|
||||||
|
'item_categories',
|
||||||
|
'item_subcategories',
|
||||||
|
'uom',
|
||||||
|
'brands',
|
||||||
|
]);
|
||||||
|
|
||||||
const normalizeString = (value) => {
|
const normalizeString = (value) => {
|
||||||
if (value === undefined || value === null) return value;
|
if (value === undefined || value === null) return value;
|
||||||
const str = String(value).trim();
|
const str = String(value).trim();
|
||||||
@ -60,13 +67,26 @@ const normalizePayload = (payload, { isCreate = false } = {}) => {
|
|||||||
const assertReference = async (table, id, label, { requireActive = true } = {}) => {
|
const assertReference = async (table, id, label, { requireActive = true } = {}) => {
|
||||||
if (!id) return null;
|
if (!id) return null;
|
||||||
const row = await prisma[table].findFirst({
|
const row = await prisma[table].findFirst({
|
||||||
where: { id: BigInt(id), deleted_at: null },
|
where: {
|
||||||
|
id: BigInt(id),
|
||||||
|
...(SOFT_DELETE_TABLES.has(table) ? { deleted_at: null } : {}),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
if (!row) throw new ApiError(422, `Invalid ${label}`);
|
if (!row) throw new ApiError(422, `Invalid ${label}`);
|
||||||
if (requireActive && row.is_active === false) throw new ApiError(422, `${label} is inactive`);
|
if (requireActive && row.is_active === false) throw new ApiError(422, `${label} is inactive`);
|
||||||
return row;
|
return row;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const assertGstRate = async (gstRateId) => {
|
||||||
|
if (!gstRateId) return null;
|
||||||
|
const row = await prisma.gst_rates.findFirst({
|
||||||
|
where: { id: BigInt(gstRateId) },
|
||||||
|
});
|
||||||
|
if (!row) throw new ApiError(422, 'Invalid gst_rate_id');
|
||||||
|
if (row.is_active === false) throw new ApiError(422, 'gst_rate_id is inactive');
|
||||||
|
return row;
|
||||||
|
};
|
||||||
|
|
||||||
const assertHsnCode = async (hsnCodeId) => {
|
const assertHsnCode = async (hsnCodeId) => {
|
||||||
if (!hsnCodeId) return null;
|
if (!hsnCodeId) return null;
|
||||||
const row = await prisma.hsn_codes.findFirst({
|
const row = await prisma.hsn_codes.findFirst({
|
||||||
@ -102,9 +122,7 @@ const validateItemPayload = async (payload, { isCreate = false } = {}) => {
|
|||||||
}
|
}
|
||||||
if (payload.uom_id) await assertReference('uom', payload.uom_id, 'uom_id');
|
if (payload.uom_id) await assertReference('uom', payload.uom_id, 'uom_id');
|
||||||
if (payload.hsn_code_id !== undefined) await assertHsnCode(payload.hsn_code_id);
|
if (payload.hsn_code_id !== undefined) await assertHsnCode(payload.hsn_code_id);
|
||||||
if (payload.gst_rate_id) {
|
if (payload.gst_rate_id !== undefined) await assertGstRate(payload.gst_rate_id);
|
||||||
await assertReference('gst_rates', payload.gst_rate_id, 'gst_rate_id', { requireActive: false });
|
|
||||||
}
|
|
||||||
if (payload.brand_id) {
|
if (payload.brand_id) {
|
||||||
await assertReference('brands', payload.brand_id, 'brand_id', { requireActive: false });
|
await assertReference('brands', payload.brand_id, 'brand_id', { requireActive: false });
|
||||||
}
|
}
|
||||||
|
|||||||
@ -90,6 +90,10 @@ const mapDbError = (err) => {
|
|||||||
if (err instanceof ApiError) return err;
|
if (err instanceof ApiError) return err;
|
||||||
if (!err) return null;
|
if (!err) return null;
|
||||||
|
|
||||||
|
if (err.name === 'PrismaClientValidationError') {
|
||||||
|
return new ApiError(500, 'Internal server error');
|
||||||
|
}
|
||||||
|
|
||||||
if (err.name === 'PrismaClientKnownRequestError' || err.code?.startsWith?.('P')) {
|
if (err.name === 'PrismaClientKnownRequestError' || err.code?.startsWith?.('P')) {
|
||||||
const mapped = mapPrismaKnownError(err);
|
const mapped = mapPrismaKnownError(err);
|
||||||
if (mapped) return mapped;
|
if (mapped) return mapped;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user