autosync_backend2 1.2.69 → 1.2.72
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +32 -10
- package/dist/index.js +92 -78
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -628,7 +628,7 @@ export declare const app: Elysia<"", {
|
|
|
628
628
|
200: {
|
|
629
629
|
totalCount: number;
|
|
630
630
|
totalPage: number;
|
|
631
|
-
result: {
|
|
631
|
+
result: Omit<{
|
|
632
632
|
product: {
|
|
633
633
|
supplierId: string;
|
|
634
634
|
productId: string | null;
|
|
@@ -660,7 +660,8 @@ export declare const app: Elysia<"", {
|
|
|
660
660
|
deletedAt: string | null;
|
|
661
661
|
oldId: number | null;
|
|
662
662
|
} | null;
|
|
663
|
-
|
|
663
|
+
totalCount: number;
|
|
664
|
+
}, "totalCount">[];
|
|
664
665
|
};
|
|
665
666
|
401: "Токен олдсонгүй";
|
|
666
667
|
422: {
|
|
@@ -1053,7 +1054,7 @@ export declare const app: Elysia<"", {
|
|
|
1053
1054
|
200: {
|
|
1054
1055
|
totalCount: number;
|
|
1055
1056
|
totalPage: number;
|
|
1056
|
-
result: {
|
|
1057
|
+
result: Omit<{
|
|
1057
1058
|
item: {
|
|
1058
1059
|
warehouseId: string;
|
|
1059
1060
|
productId: string;
|
|
@@ -1100,7 +1101,8 @@ export declare const app: Elysia<"", {
|
|
|
1100
1101
|
deletedAt: string | null;
|
|
1101
1102
|
oldId: number | null;
|
|
1102
1103
|
};
|
|
1103
|
-
|
|
1104
|
+
totalCount: number;
|
|
1105
|
+
}, "totalCount">[];
|
|
1104
1106
|
};
|
|
1105
1107
|
401: "Токен олдсонгүй";
|
|
1106
1108
|
403: "Хандах эрхгүй байна.";
|
|
@@ -1125,9 +1127,7 @@ export declare const app: Elysia<"", {
|
|
|
1125
1127
|
query: unknown;
|
|
1126
1128
|
headers: unknown;
|
|
1127
1129
|
response: {
|
|
1128
|
-
200:
|
|
1129
|
-
count: number;
|
|
1130
|
-
};
|
|
1130
|
+
200: number;
|
|
1131
1131
|
401: "Токен олдсонгүй";
|
|
1132
1132
|
403: "Хандах эрхгүй байна.";
|
|
1133
1133
|
};
|
|
@@ -1201,10 +1201,15 @@ export declare const app: Elysia<"", {
|
|
|
1201
1201
|
headers: unknown;
|
|
1202
1202
|
response: {
|
|
1203
1203
|
200: AsyncGenerator<import("elysia").ElysiaCustomStatusResponse<"Not Found", "Агуулахын бараа олдсонгүй.", 404> | import("elysia").ElysiaCustomStatusResponse<"Bad Request", "Ажилтны мэдээлэл шаардлагатай.", 400> | import("elysia").ElysiaCustomStatusResponse<"Bad Request", "Барааны үлдэгдэл хүрэлцэхгүй байна.", 400> | {
|
|
1204
|
+
type: "New Items";
|
|
1205
|
+
percent: number;
|
|
1206
|
+
count: number;
|
|
1207
|
+
total: number;
|
|
1208
|
+
} | {
|
|
1209
|
+
type: "Current Items";
|
|
1204
1210
|
percent: number;
|
|
1205
1211
|
count: number;
|
|
1206
1212
|
total: number;
|
|
1207
|
-
type: string;
|
|
1208
1213
|
}, void, unknown>;
|
|
1209
1214
|
401: "Токен олдсонгүй";
|
|
1210
1215
|
403: "Хандах эрхгүй байна.";
|
|
@@ -1260,7 +1265,8 @@ export declare const app: Elysia<"", {
|
|
|
1260
1265
|
200: {
|
|
1261
1266
|
totalCount: number;
|
|
1262
1267
|
totalPage: number;
|
|
1263
|
-
result: {
|
|
1268
|
+
result: Omit<{
|
|
1269
|
+
totalCount: number;
|
|
1264
1270
|
product: {
|
|
1265
1271
|
supplierId: string;
|
|
1266
1272
|
productId: string | null;
|
|
@@ -1319,7 +1325,7 @@ export declare const app: Elysia<"", {
|
|
|
1319
1325
|
updatedAt: string;
|
|
1320
1326
|
deletedAt: string | null;
|
|
1321
1327
|
oldId: number | null;
|
|
1322
|
-
}[];
|
|
1328
|
+
}, "totalCount">[];
|
|
1323
1329
|
};
|
|
1324
1330
|
401: "Токен олдсонгүй";
|
|
1325
1331
|
403: "Хандах эрхгүй байна.";
|
|
@@ -5191,6 +5197,22 @@ export declare const app: Elysia<"", {
|
|
|
5191
5197
|
deletedAt: string | null;
|
|
5192
5198
|
oldId: number | null;
|
|
5193
5199
|
} | null;
|
|
5200
|
+
item: {
|
|
5201
|
+
cpOrderId: string;
|
|
5202
|
+
companyProductId: string | null;
|
|
5203
|
+
companyServiceKindId: string | null;
|
|
5204
|
+
cpOrderSpPackageId: string | null;
|
|
5205
|
+
quantity: number;
|
|
5206
|
+
priceUnit: number;
|
|
5207
|
+
priceTotal: number;
|
|
5208
|
+
name: string | null;
|
|
5209
|
+
description: string | null;
|
|
5210
|
+
id: string;
|
|
5211
|
+
createdAt: string;
|
|
5212
|
+
updatedAt: string;
|
|
5213
|
+
deletedAt: string | null;
|
|
5214
|
+
oldId: number | null;
|
|
5215
|
+
} | null;
|
|
5194
5216
|
}[];
|
|
5195
5217
|
};
|
|
5196
5218
|
401: "Токен олдсонгүй";
|
package/dist/index.js
CHANGED
|
@@ -147984,8 +147984,15 @@ var import_exceljs = __toESM(require_excel(), 1);
|
|
|
147984
147984
|
var WarehouseProductLogic;
|
|
147985
147985
|
((WarehouseProductLogic) => {
|
|
147986
147986
|
WarehouseProductLogic.getFilter = (query, user2) => {
|
|
147987
|
-
const
|
|
147988
|
-
|
|
147987
|
+
const search = [];
|
|
147988
|
+
if (query.search) {
|
|
147989
|
+
const splitted = query.search.trim().split(" ").filter((i2) => i2 !== "");
|
|
147990
|
+
const productKeys = ["name", "barCode", "oen", "partNumber", "alias"];
|
|
147991
|
+
for (const key of productKeys) {
|
|
147992
|
+
search.push(and(...splitted.map((i2) => ilike(warehouseProductTable[key], `%${i2}%`))));
|
|
147993
|
+
}
|
|
147994
|
+
}
|
|
147995
|
+
return and(softDeletedFilter(warehouseProductTable), eq(warehouseProductTable.companyId, user2.companyId), or(...search)?.if(!!query.search), eq(warehouseProductTable.priceSell, query.priceSell ?? 0).if(query.priceSell), eq(warehouseProductTable.productId, query.productId ?? "").if(query.productId), eq(warehouseProductTable.supplierId, query.supplierId ?? "").if(query.supplierId), eq(warehouseProductTable.id, query.id ?? "").if(query.id), inArray(warehouseProductTable.id, query.ids ?? []).if(query.ids?.length), ...query.type ? [eq(warehouseProductTable.type, query.type)] : []);
|
|
147989
147996
|
};
|
|
147990
147997
|
WarehouseProductLogic.select = async (query, user2) => {
|
|
147991
147998
|
const baseQuery = db_default.select({
|
|
@@ -147994,14 +148001,7 @@ var WarehouseProductLogic;
|
|
|
147994
148001
|
totalCount: totalCountSql
|
|
147995
148002
|
}).from(warehouseProductTable).leftJoin(techdocSupplierTable, eq(techdocSupplierTable.id, warehouseProductTable.supplierId)).where(WarehouseProductLogic.getFilter(query, user2)).orderBy(desc(warehouseProductTable.createdAt)).$dynamic();
|
|
147996
148003
|
const result = await pagination_helper_default(baseQuery, query.pagination);
|
|
147997
|
-
|
|
147998
|
-
totalCount: result[0]?.totalCount ?? 0,
|
|
147999
|
-
totalPage: result.length === 0 ? 0 : Math.ceil(result[0].totalCount / query.pagination.size)
|
|
148000
|
-
};
|
|
148001
|
-
return {
|
|
148002
|
-
result: result.map(({ totalCount, ...item }) => item),
|
|
148003
|
-
...content
|
|
148004
|
-
};
|
|
148004
|
+
return getPaginationContent(result, query.pagination.size);
|
|
148005
148005
|
};
|
|
148006
148006
|
WarehouseProductLogic.create = async (body, user2) => {
|
|
148007
148007
|
const [result] = await db_default.insert(warehouseProductTable).values({
|
|
@@ -148109,10 +148109,22 @@ var WarehouseProductLogic;
|
|
|
148109
148109
|
var logic_default8 = WarehouseProductLogic;
|
|
148110
148110
|
|
|
148111
148111
|
// src/routes/warehouse/item/logic.ts
|
|
148112
|
+
var BATCH_SIZE = 1000;
|
|
148113
|
+
var DATA_STORE_EXPIRY_HOURS = 1;
|
|
148114
|
+
var TRANSACTION_DESCRIPTION_IN = "\u0411\u0430\u0440\u0430\u0430 \u043E\u0440\u043B\u043E\u0433\u0434\u043E\u0445";
|
|
148115
|
+
var ERRORS = {
|
|
148116
|
+
EMPLOYEE_REQUIRED: "\u0410\u0436\u0438\u043B\u0442\u043D\u044B \u043C\u044D\u0434\u044D\u044D\u043B\u044D\u043B \u0448\u0430\u0430\u0440\u0434\u043B\u0430\u0433\u0430\u0442\u0430\u0439.",
|
|
148117
|
+
ITEM_NOT_FOUND: "\u0410\u0433\u0443\u0443\u043B\u0430\u0445\u044B\u043D \u0431\u0430\u0440\u0430\u0430 \u043E\u043B\u0434\u0441\u043E\u043D\u0433\u04AF\u0439.",
|
|
148118
|
+
PRODUCT_NOT_FOUND: "\u0411\u0430\u0440\u0430\u0430\u043D\u044B \u043C\u044D\u0434\u044D\u044D\u043B\u044D\u043B \u043E\u043B\u0434\u0441\u043E\u043D\u0433\u04AF\u0439.",
|
|
148119
|
+
INSUFFICIENT_STOCK: "\u0411\u0430\u0440\u0430\u0430\u043D\u044B \u04AF\u043B\u0434\u044D\u0433\u0434\u044D\u043B \u0445\u04AF\u0440\u044D\u043B\u0446\u044D\u0445\u0433\u04AF\u0439 \u0431\u0430\u0439\u043D\u0430."
|
|
148120
|
+
};
|
|
148112
148121
|
var WarehouseItemLogic;
|
|
148113
148122
|
((WarehouseItemLogic) => {
|
|
148123
|
+
const buildItemFilter = (query, user2) => {
|
|
148124
|
+
return and(softDeletedFilter(warehouseItemTable).if(query.isActive), isNotNull(warehouseItemTable.deletedAt).if(query.isActive !== undefined && query.isActive === false), ...query.product ? [logic_default8.getFilter(query.product, user2)] : [], eq(warehouseItemTable.safetyStock, query.safetyStock ?? 0).if(query.safetyStock), lte(warehouseItemTable.quantity, warehouseItemTable.safetyStock).if(query.isSafetyStock), ilike(warehouseItemTable.shelfNumber, `%${query.shelfNumber}%`).if(query.shelfNumber), eq(warehouseItemTable.warehouseId, query.warehouseId).if(query.warehouseId), eq(warehouseItemTable.isOrdered, query.isOrdered).if(query.isOrdered));
|
|
148125
|
+
};
|
|
148114
148126
|
WarehouseItemLogic.select = async (query, user2) => {
|
|
148115
|
-
const filter =
|
|
148127
|
+
const filter = buildItemFilter(query, user2);
|
|
148116
148128
|
const baseQuery = db_default.select({
|
|
148117
148129
|
item: warehouseItemTable,
|
|
148118
148130
|
supplier: techdocSupplierTable,
|
|
@@ -148120,21 +148132,14 @@ var WarehouseItemLogic;
|
|
|
148120
148132
|
totalCount: totalCountSql
|
|
148121
148133
|
}).from(warehouseItemTable).where(filter).innerJoin(warehouseProductTable, and(eq(warehouseItemTable.productId, warehouseProductTable.id), isNull2(warehouseProductTable.deletedAt))).leftJoin(techdocSupplierTable, eq(warehouseProductTable.supplierId, techdocSupplierTable.id)).orderBy(warehouseItemTable.order, warehouseItemTable.createdAt).$dynamic();
|
|
148122
148134
|
const result = await pagination_helper_default(baseQuery, query.pagination);
|
|
148123
|
-
|
|
148124
|
-
totalCount: result[0]?.totalCount ?? 0,
|
|
148125
|
-
totalPage: result.length === 0 ? 0 : Math.ceil(result[0].totalCount / query.pagination.size)
|
|
148126
|
-
};
|
|
148127
|
-
return {
|
|
148128
|
-
result: result.map(({ totalCount, ...item }) => item),
|
|
148129
|
-
...content
|
|
148130
|
-
};
|
|
148135
|
+
return getPaginationContent(result, query.pagination.size);
|
|
148131
148136
|
};
|
|
148132
148137
|
WarehouseItemLogic.selectSos = async (user2) => {
|
|
148133
148138
|
const filter = and(softDeletedFilter(warehouseItemTable), lte(warehouseItemTable.quantity, warehouseItemTable.safetyStock), logic_default8.getFilter({}, user2));
|
|
148134
148139
|
const result = await db_default.select({
|
|
148135
148140
|
count: sql3`count(*)`.mapWith(Number)
|
|
148136
148141
|
}).from(warehouseItemTable).innerJoin(warehouseProductTable, eq(warehouseItemTable.productId, warehouseProductTable.id)).where(filter);
|
|
148137
|
-
return result[0] ?? 0;
|
|
148142
|
+
return result[0]?.count ?? 0;
|
|
148138
148143
|
};
|
|
148139
148144
|
WarehouseItemLogic.create = async (body) => {
|
|
148140
148145
|
const [result] = await db_default.insert(warehouseItemTable).values(body).returning();
|
|
@@ -148143,21 +148148,22 @@ var WarehouseItemLogic;
|
|
|
148143
148148
|
WarehouseItemLogic.update = async (id, body) => {
|
|
148144
148149
|
const [result] = await db_default.update(warehouseItemTable).set(body).where(eq(warehouseItemTable.id, id)).returning();
|
|
148145
148150
|
if (!result) {
|
|
148146
|
-
return status("Not Found",
|
|
148151
|
+
return status("Not Found", ERRORS.ITEM_NOT_FOUND);
|
|
148147
148152
|
}
|
|
148148
148153
|
return result;
|
|
148149
148154
|
};
|
|
148150
148155
|
WarehouseItemLogic.upload = async (body) => {
|
|
148151
|
-
const
|
|
148156
|
+
const productIds = body.map((item) => item.productId);
|
|
148157
|
+
const products = await logic_default8.selectByIds(productIds);
|
|
148152
148158
|
if (products.length !== body.length) {
|
|
148153
|
-
const
|
|
148159
|
+
const notFoundProductIds = body.filter((item) => !products.some((p) => p.id === item.productId)).map((item) => item.productId);
|
|
148154
148160
|
return status("Bad Request", {
|
|
148155
|
-
message:
|
|
148156
|
-
data:
|
|
148161
|
+
message: ERRORS.PRODUCT_NOT_FOUND,
|
|
148162
|
+
data: notFoundProductIds
|
|
148157
148163
|
});
|
|
148158
148164
|
}
|
|
148159
148165
|
const result = await dataStore_logic_default.create({
|
|
148160
|
-
expireDate: sql3`now() + interval '
|
|
148166
|
+
expireDate: sql3`now() + interval '${DATA_STORE_EXPIRY_HOURS} hour'`,
|
|
148161
148167
|
data: {
|
|
148162
148168
|
warehouseInId: body[0].warehouseId,
|
|
148163
148169
|
transactionKind: "ORDER",
|
|
@@ -148166,62 +148172,76 @@ var WarehouseItemLogic;
|
|
|
148166
148172
|
});
|
|
148167
148173
|
return result;
|
|
148168
148174
|
};
|
|
148175
|
+
const createInitialTransaction = (item, userId, employeeId) => ({
|
|
148176
|
+
itemId: item.id,
|
|
148177
|
+
productId: item.productId,
|
|
148178
|
+
transactionType: "IN",
|
|
148179
|
+
transactionKind: "TRANSFER",
|
|
148180
|
+
quantity: item.quantity,
|
|
148181
|
+
quantityBefore: 0,
|
|
148182
|
+
quantityAfter: item.quantity,
|
|
148183
|
+
description: TRANSACTION_DESCRIPTION_IN,
|
|
148184
|
+
employeeId,
|
|
148185
|
+
userId
|
|
148186
|
+
});
|
|
148187
|
+
const separateNewAndExistingItems = async (body) => {
|
|
148188
|
+
const productIds = body.map((item) => item.productId);
|
|
148189
|
+
const currentItems = await db_default.select().from(warehouseItemTable).where(inArray(warehouseItemTable.productId, productIds));
|
|
148190
|
+
const currentItemKeys = new Set(currentItems.map((item) => `${item.productId}-${item.warehouseId}`));
|
|
148191
|
+
const newItems = body.filter((item) => !currentItemKeys.has(`${item.productId}-${item.warehouseId}`));
|
|
148192
|
+
return { newItems, currentItems };
|
|
148193
|
+
};
|
|
148194
|
+
const processNewItemsBatch = async (batch, user2, employeeId) => {
|
|
148195
|
+
const inserted = await db_default.insert(warehouseItemTable).values(batch).returning();
|
|
148196
|
+
await db_default.insert(warehouseItemTransactionTable).values(inserted.map((item) => createInitialTransaction(item, user2.id, employeeId)));
|
|
148197
|
+
return inserted;
|
|
148198
|
+
};
|
|
148199
|
+
const calculateProgress = (current, total) => ({
|
|
148200
|
+
percent: Math.min(Math.round(current / total * 100), 100),
|
|
148201
|
+
count: current,
|
|
148202
|
+
total
|
|
148203
|
+
});
|
|
148169
148204
|
WarehouseItemLogic.createMany = async function* (body, user2) {
|
|
148170
148205
|
if (!user2.employeeId) {
|
|
148171
|
-
throw status("Bad Request",
|
|
148206
|
+
throw status("Bad Request", ERRORS.EMPLOYEE_REQUIRED);
|
|
148172
148207
|
}
|
|
148173
|
-
const
|
|
148174
|
-
const currentItems = await db_default.select().from(warehouseItemTable).where(inArray(warehouseItemTable.productId, body.map((item) => item.productId)));
|
|
148175
|
-
const newItems = body.filter((item) => currentItems.filter((i2) => i2.productId === item.productId && i2.warehouseId === item.warehouseId).length === 0);
|
|
148208
|
+
const { newItems, currentItems } = await separateNewAndExistingItems(body);
|
|
148176
148209
|
for (let i2 = 0;i2 < newItems.length; i2 += BATCH_SIZE) {
|
|
148177
148210
|
const batch = newItems.slice(i2, i2 + BATCH_SIZE);
|
|
148178
|
-
|
|
148179
|
-
await db_default.insert(warehouseItemTransactionTable).values(inserted.map((i3) => ({
|
|
148180
|
-
itemId: i3.id,
|
|
148181
|
-
productId: i3.productId,
|
|
148182
|
-
transactionType: "IN",
|
|
148183
|
-
transactionKind: "TRANSFER",
|
|
148184
|
-
quantity: i3.quantity,
|
|
148185
|
-
quantityBefore: 0,
|
|
148186
|
-
quantityAfter: i3.quantity,
|
|
148187
|
-
description: "\u0411\u0430\u0440\u0430\u0430 \u043E\u0440\u043B\u043E\u0433\u0434\u043E\u0445",
|
|
148188
|
-
employeeId: user2.employeeId ?? "",
|
|
148189
|
-
userId: user2.id
|
|
148190
|
-
})));
|
|
148191
|
-
const percent = Math.round((i2 * BATCH_SIZE + batch.length) / newItems.length * 100);
|
|
148211
|
+
await processNewItemsBatch(batch, user2, user2.employeeId);
|
|
148192
148212
|
yield {
|
|
148193
|
-
|
|
148194
|
-
count: i2,
|
|
148195
|
-
total: body.length,
|
|
148213
|
+
...calculateProgress(i2 + batch.length, newItems.length),
|
|
148196
148214
|
type: "New Items"
|
|
148197
148215
|
};
|
|
148198
148216
|
}
|
|
148199
|
-
for (const [index2,
|
|
148200
|
-
const
|
|
148201
|
-
|
|
148202
|
-
|
|
148203
|
-
|
|
148204
|
-
|
|
148205
|
-
|
|
148206
|
-
|
|
148207
|
-
|
|
148217
|
+
for (const [index2, currentItem] of currentItems.entries()) {
|
|
148218
|
+
const matchingBodyItem = body.find((item) => item.productId === currentItem.productId && item.warehouseId === currentItem.warehouseId);
|
|
148219
|
+
if (matchingBodyItem && matchingBodyItem.quantity !== undefined) {
|
|
148220
|
+
const result = await WarehouseItemLogic.changeQuantity({
|
|
148221
|
+
itemId: currentItem.id,
|
|
148222
|
+
quantityChange: matchingBodyItem.quantity,
|
|
148223
|
+
transactionDetails: {
|
|
148224
|
+
transactionType: "IN",
|
|
148225
|
+
transactionKind: "TRANSFER",
|
|
148226
|
+
description: TRANSACTION_DESCRIPTION_IN
|
|
148227
|
+
}
|
|
148228
|
+
}, user2);
|
|
148229
|
+
if ("code" in result) {
|
|
148230
|
+
yield result;
|
|
148208
148231
|
}
|
|
148209
|
-
}, user2);
|
|
148210
|
-
if ("code" in res) {
|
|
148211
|
-
yield res;
|
|
148212
148232
|
}
|
|
148213
|
-
const percent = Math.round(index2 / currentItems.length * 100);
|
|
148214
148233
|
yield {
|
|
148215
|
-
|
|
148216
|
-
count: index2,
|
|
148217
|
-
total: currentItems.length,
|
|
148234
|
+
...calculateProgress(index2 + 1, currentItems.length),
|
|
148218
148235
|
type: "Current Items"
|
|
148219
148236
|
};
|
|
148220
148237
|
}
|
|
148221
148238
|
};
|
|
148239
|
+
const buildTransactionFilter = (query, user2) => {
|
|
148240
|
+
return and(softDeletedFilter(warehouseItemTransactionTable), eq(user.companyId, user2.companyId), ...query.product ? [logic_default8.getFilter(query.product, user2)] : [], eq(warehouseItemTransactionTable.transactionType, query.transactionType ?? "IN").if(query.transactionType), eq(warehouseItemTransactionTable.transactionKind, query.transactionKind ?? "TRANSFER").if(query.transactionKind), eq(warehouseItemTransactionTable.employeeId, query.employeeId ?? "").if(query.employeeId), eq(warehouseItemTransactionTable.userId, query.userId ?? "").if(query.userId), eq(warehouseItemTransactionTable.salesId, query.salesId ?? "").if(query.salesId), eq(warehouseItemTransactionTable.orderId, query.orderId ?? "").if(query.orderId), eq(warehouseItemTransactionTable.transferId, query.transferId ?? "").if(query.transferId), eq(warehouseItemTransactionTable.itemId, query.itemId ?? "").if(query.itemId), gte(warehouseItemTransactionTable.createdAt, query.dateFrom ?? "").if(query.dateFrom), lte(warehouseItemTransactionTable.createdAt, query.dateTo ?? "").if(query.dateTo), gte(warehouseItemTransactionTable.quantity, query.minQuantity ?? 0).if(query.minQuantity), lte(warehouseItemTransactionTable.quantity, query.maxQuantity ?? 0).if(query.maxQuantity), eq(warehouseItemTable.warehouseId, query.warehouseId).if(query.warehouseId));
|
|
148241
|
+
};
|
|
148222
148242
|
WarehouseItemLogic.selectTransaction = async (query, user2) => {
|
|
148243
|
+
const filter = buildTransactionFilter(query, user2);
|
|
148223
148244
|
const columns = getTableColumns(warehouseItemTransactionTable);
|
|
148224
|
-
const filter = and(softDeletedFilter(warehouseItemTransactionTable), eq(user.companyId, user2.companyId), ...query.product ? [logic_default8.getFilter(query.product, user2)] : [], eq(warehouseItemTransactionTable.transactionType, query.transactionType ?? "IN").if(query.transactionType), eq(warehouseItemTransactionTable.transactionKind, query.transactionKind ?? "TRANSFER").if(query.transactionKind), eq(warehouseItemTransactionTable.employeeId, query.employeeId ?? "").if(query.employeeId), eq(warehouseItemTransactionTable.userId, query.userId ?? "").if(query.userId), eq(warehouseItemTransactionTable.salesId, query.salesId ?? "").if(query.salesId), eq(warehouseItemTransactionTable.orderId, query.orderId ?? "").if(query.orderId), eq(warehouseItemTransactionTable.transferId, query.transferId ?? "").if(query.transferId), eq(warehouseItemTransactionTable.itemId, query.itemId ?? "").if(query.itemId), gte(warehouseItemTransactionTable.createdAt, query.dateFrom ?? "").if(query.dateFrom), lte(warehouseItemTransactionTable.createdAt, query.dateTo ?? "").if(query.dateTo), gte(warehouseItemTransactionTable.quantity, query.minQuantity ?? 0).if(query.minQuantity), lte(warehouseItemTransactionTable.quantity, query.maxQuantity ?? 0).if(query.maxQuantity), eq(warehouseItemTable.warehouseId, query.warehouseId).if(query.warehouseId));
|
|
148225
148245
|
const baseQuery = db_default.select({
|
|
148226
148246
|
...columns,
|
|
148227
148247
|
totalCount: totalCountSql,
|
|
@@ -148233,14 +148253,7 @@ var WarehouseItemLogic;
|
|
|
148233
148253
|
}
|
|
148234
148254
|
}).from(warehouseItemTransactionTable).where(filter).leftJoin(warehouseProductTable, eq(warehouseItemTransactionTable.productId, warehouseProductTable.id)).leftJoin(warehouseItemTable, eq(warehouseItemTransactionTable.itemId, warehouseItemTable.id)).leftJoin(user, eq(warehouseItemTransactionTable.userId, user.id)).orderBy(desc(warehouseItemTransactionTable.createdAt)).$dynamic();
|
|
148235
148255
|
const result = await pagination_helper_default(baseQuery, query.pagination);
|
|
148236
|
-
|
|
148237
|
-
totalCount: result[0]?.totalCount ?? 0,
|
|
148238
|
-
totalPage: result.length === 0 ? 0 : Math.ceil(result[0].totalCount / query.pagination.size)
|
|
148239
|
-
};
|
|
148240
|
-
return {
|
|
148241
|
-
result: result.map(({ totalCount, ...item }) => item),
|
|
148242
|
-
...content
|
|
148243
|
-
};
|
|
148256
|
+
return getPaginationContent(result, query.pagination.size);
|
|
148244
148257
|
};
|
|
148245
148258
|
WarehouseItemLogic.changeQuantity = async ({
|
|
148246
148259
|
itemId,
|
|
@@ -148248,16 +148261,16 @@ var WarehouseItemLogic;
|
|
|
148248
148261
|
transactionDetails
|
|
148249
148262
|
}, user2, tdb = db_default) => {
|
|
148250
148263
|
if (!user2.employeeId) {
|
|
148251
|
-
return status("Bad Request",
|
|
148264
|
+
return status("Bad Request", ERRORS.EMPLOYEE_REQUIRED);
|
|
148252
148265
|
}
|
|
148253
148266
|
const [currentItem] = await tdb.select().from(warehouseItemTable).where(eq(warehouseItemTable.id, itemId)).limit(1);
|
|
148254
148267
|
if (!currentItem) {
|
|
148255
|
-
return status("Not Found",
|
|
148268
|
+
return status("Not Found", ERRORS.ITEM_NOT_FOUND);
|
|
148256
148269
|
}
|
|
148257
148270
|
const quantityBefore = currentItem.quantity;
|
|
148258
148271
|
const quantityAfter = quantityBefore + quantityChange;
|
|
148259
148272
|
if (transactionDetails.transactionType === "OUT" && quantityAfter < 0) {
|
|
148260
|
-
return status("Bad Request",
|
|
148273
|
+
return status("Bad Request", ERRORS.INSUFFICIENT_STOCK);
|
|
148261
148274
|
}
|
|
148262
148275
|
const [updatedItem] = await tdb.update(warehouseItemTable).set({
|
|
148263
148276
|
quantity: quantityAfter,
|
|
@@ -152148,8 +152161,9 @@ var DiscountLogic;
|
|
|
152148
152161
|
DiscountLogic.selectAppliesByCpOrderId = async (cpOrderId) => {
|
|
152149
152162
|
const appliedDiscounts = await db_default.select({
|
|
152150
152163
|
apply: crmDiscountApplyTable,
|
|
152151
|
-
discount: crmDiscountTable
|
|
152152
|
-
|
|
152164
|
+
discount: crmDiscountTable,
|
|
152165
|
+
item: crmCpOrderItemTable
|
|
152166
|
+
}).from(crmDiscountApplyTable).leftJoin(crmDiscountTable, eq(crmDiscountTable.id, crmDiscountApplyTable.discountId)).leftJoin(crmCpOrderItemTable, eq(crmCpOrderItemTable.id, crmDiscountApplyTable.cpOrderItemId)).where(Array.isArray(cpOrderId) ? inArray(crmDiscountApplyTable.cpOrderId, cpOrderId) : eq(crmDiscountApplyTable.cpOrderId, cpOrderId)).orderBy(crmDiscountApplyTable.createdAt);
|
|
152153
152167
|
return appliedDiscounts;
|
|
152154
152168
|
};
|
|
152155
152169
|
DiscountLogic.create = async (body, user2) => {
|
|
@@ -156355,7 +156369,7 @@ var WarehouseWarehouseLogic;
|
|
|
156355
156369
|
return result;
|
|
156356
156370
|
};
|
|
156357
156371
|
WarehouseWarehouseLogic.update = async (id, body, user2) => {
|
|
156358
|
-
const [result] = await db_default.update(warehouseWarehouseTable).set(body).where(and(eq(warehouseWarehouseTable.id, id), eq(warehouseWarehouseTable.companyId, user2.companyId), eq(warehouseWarehouseTable.branchId, user2.branchId))).returning();
|
|
156372
|
+
const [result] = await db_default.update(warehouseWarehouseTable).set(body).where(and(eq(warehouseWarehouseTable.id, id), eq(warehouseWarehouseTable.companyId, user2.companyId), eq(warehouseWarehouseTable.branchId, user2.branchId).if(user2.kind === "CUSTOMER"))).returning();
|
|
156359
156373
|
if (!result) {
|
|
156360
156374
|
return status("Not Found", "\u0410\u0433\u0443\u0443\u043B\u0430\u0445 \u043E\u043B\u0434\u0441\u043E\u043D\u0433\u04AF\u0439.");
|
|
156361
156375
|
}
|