products
This commit is contained in:
parent
e45be71606
commit
b0dc1672fc
3 changed files with 156 additions and 21 deletions
|
@ -2,6 +2,7 @@ import { and, coalesce, eq, insert, max, plus, remove, select, update } from "db
|
|||
import { checkSelfTag } from "../../server/outbagURL.js";
|
||||
import { Act, Client, STATE } from "../user.js";
|
||||
import { db, listCategories, listProducts } from "../../sys/db.js";
|
||||
import { isCategoryInRoom, isProductInRoom } from "../helper.js"
|
||||
|
||||
export const getCategories: Act = {
|
||||
state: STATE.client | STATE.remote,
|
||||
|
@ -90,7 +91,7 @@ export const changeCategory: Act = {
|
|||
data: {
|
||||
room: "string",
|
||||
server: "string",
|
||||
catID: "number",
|
||||
listCatID: "number",
|
||||
title: "string-256",
|
||||
color: "string-32"
|
||||
},
|
||||
|
@ -111,7 +112,7 @@ export const changeCategory: Act = {
|
|||
.set(listCategories.color, data.color)
|
||||
.where(and(
|
||||
eq(listCategories.roomID, roomID),
|
||||
eq(listCategories.listCatID, data.catID)
|
||||
eq(listCategories.listCatID, data.listCatID)
|
||||
))
|
||||
.query(db);
|
||||
if (req.affectedRows > 0) aws("ok", "");
|
||||
|
@ -125,7 +126,7 @@ export const changeCategoryWeights: Act = {
|
|||
data: {
|
||||
room: "string",
|
||||
server: "string",
|
||||
catIDs: "array-number",
|
||||
listCatIDs: "array-number",
|
||||
weights: "array-number",
|
||||
},
|
||||
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
|
||||
|
@ -140,15 +141,15 @@ export const changeCategoryWeights: Act = {
|
|||
aws("error", "existence");
|
||||
return;
|
||||
}
|
||||
if (data.weights.length != data.catIDs.length) return void aws("error", "data");
|
||||
if (data.weights.length != data.listCatIDs.length) return void aws("error", "data");
|
||||
let affacted = 0;
|
||||
for (let i = 0; i < data.catIDs.length; i++) {
|
||||
const catID = data.catIDs[i];
|
||||
for (let i = 0; i < data.listCatIDs.length; i++) {
|
||||
const listCatID = data.listCatIDs[i];
|
||||
let req = await update(listCategories)
|
||||
.set(listCategories.weight, data.title)
|
||||
.where(and(
|
||||
eq(listCategories.roomID, roomID),
|
||||
eq(listCategories.listCatID, catID)
|
||||
eq(listCategories.listCatID, listCatID)
|
||||
))
|
||||
.query(db);
|
||||
affacted += req.affectedRows;
|
||||
|
@ -164,7 +165,7 @@ export const deleteCategory: Act = {
|
|||
data: {
|
||||
room: "string",
|
||||
server: "string",
|
||||
catID: "number",
|
||||
listCatID: "number",
|
||||
},
|
||||
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
|
||||
if (!checkSelfTag(data.server)) {
|
||||
|
@ -181,7 +182,7 @@ export const deleteCategory: Act = {
|
|||
let req = await remove(listCategories)
|
||||
.where(and(
|
||||
eq(listCategories.roomID, roomID),
|
||||
eq(listCategories.listCatID, data.catID)
|
||||
eq(listCategories.listCatID, data.listCatID)
|
||||
))
|
||||
.query(db);
|
||||
if (req.affectedRows > 0) aws("ok", "");
|
||||
|
@ -233,3 +234,124 @@ export const getProducts: Act = {
|
|||
}
|
||||
};
|
||||
|
||||
export const addProduct: Act = {
|
||||
state: STATE.client | STATE.remote,
|
||||
right: 0,
|
||||
data: {
|
||||
room: "string",
|
||||
server: "string",
|
||||
title: "string-256",
|
||||
description: "string-4096",
|
||||
listCatID: "number",
|
||||
defUnit: "number",
|
||||
defValue: "string-256",
|
||||
ean: "string-64"
|
||||
},
|
||||
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
|
||||
if (!checkSelfTag(data.server)) {
|
||||
if (client.state != STATE.client) return void aws("error", "right");
|
||||
let resp = await client.pass(data.server, "addProduct", data);
|
||||
aws(resp.state, resp.data);
|
||||
return;
|
||||
}
|
||||
let roomID = await client.isInRoom(data.room);
|
||||
if (roomID == -1) {
|
||||
aws("error", "existence");
|
||||
return;
|
||||
}
|
||||
if (!isCategoryInRoom(roomID, data.listCatID)) return void aws("error", "existence");
|
||||
let req = await insert(
|
||||
listProducts.roomID,
|
||||
listProducts.title,
|
||||
listProducts.description,
|
||||
listProducts.category,
|
||||
listProducts.defUnit,
|
||||
listProducts.defValue,
|
||||
listProducts.ean,
|
||||
).add(
|
||||
roomID,
|
||||
data.title,
|
||||
data.description,
|
||||
data.listCatID,
|
||||
data.defUnit,
|
||||
data.defValue,
|
||||
data.ean
|
||||
).query(db);
|
||||
if (req.affectedRows > 0) aws("ok", {
|
||||
listProdID: req.insertId
|
||||
});
|
||||
else aws("error", "existence");
|
||||
}
|
||||
};
|
||||
|
||||
export const changeProduct: Act = {
|
||||
state: STATE.client | STATE.remote,
|
||||
right: 0,
|
||||
data: {
|
||||
room: "string",
|
||||
server: "string",
|
||||
listProdID: "number",
|
||||
title: "string-256",
|
||||
description: "string-4096",
|
||||
listCatID: "number",
|
||||
defUnit: "number",
|
||||
defValue: "string-256",
|
||||
ean: "string-64"
|
||||
},
|
||||
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
|
||||
if (!checkSelfTag(data.server)) {
|
||||
if (client.state != STATE.client) return void aws("error", "right");
|
||||
let resp = await client.pass(data.server, "changeProduct", data);
|
||||
aws(resp.state, resp.data);
|
||||
return;
|
||||
}
|
||||
let roomID = await client.isInRoom(data.room);
|
||||
if (roomID == -1) {
|
||||
aws("error", "existence");
|
||||
return;
|
||||
}
|
||||
if (!isCategoryInRoom(roomID, data.listCatID)) return void aws("error", "existence");
|
||||
if (!isProductInRoom(roomID, data.listProdID)) return void aws("error", "existence");
|
||||
let req = await update(listProducts)
|
||||
.set(listProducts.title, data.title)
|
||||
.set(listProducts.description, data.description)
|
||||
.set(listProducts.category, data.listCatID)
|
||||
.set(listProducts.defUnit, data.defUnit)
|
||||
.set(listProducts.defValue, data.defValue)
|
||||
.set(listProducts.ean, data.ean)
|
||||
.where(eq(listProducts.listProdID, data.listProdID))
|
||||
.query(db);
|
||||
if (req.affectedRows > 0) aws("ok", "");
|
||||
else aws("error", "existence");
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteProduct: Act = {
|
||||
state: STATE.client | STATE.remote,
|
||||
right: 0,
|
||||
data: {
|
||||
room: "string",
|
||||
server: "string",
|
||||
listProdID: "number"
|
||||
},
|
||||
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
|
||||
if (!checkSelfTag(data.server)) {
|
||||
if (client.state != STATE.client) return void aws("error", "right");
|
||||
let resp = await client.pass(data.server, "deleteProduct", data);
|
||||
aws(resp.state, resp.data);
|
||||
return;
|
||||
}
|
||||
let roomID = await client.isInRoom(data.room);
|
||||
if (roomID == -1) {
|
||||
aws("error", "existence");
|
||||
return;
|
||||
}
|
||||
if (!isCategoryInRoom(roomID, data.listCatID)) return void aws("error", "existence");
|
||||
if (!isProductInRoom(roomID, data.listProdID)) return void aws("error", "existence");
|
||||
let req = await remove(listProducts)
|
||||
.where(eq(listProducts.listProdID, data.listProdID))
|
||||
.query(db);
|
||||
if (req.affectedRows > 0) aws("ok", "");
|
||||
else aws("error", "existence");
|
||||
}
|
||||
};
|
|
@ -1,5 +1,5 @@
|
|||
import { alias, eq, count, select, naturalJoin, innerJoinOn } from "dblang";
|
||||
import { accounts, db, roomMembers, rooms } from "../sys/db.js";
|
||||
import { alias, eq, count, select, innerJoinOn } from "dblang";
|
||||
import { accounts, db, listCategories, listProducts, roomMembers, rooms } from "../sys/db.js";
|
||||
|
||||
export const isRoomFull = async (roomID: number) => {
|
||||
let currentCount = alias(
|
||||
|
@ -15,7 +15,7 @@ export const isRoomFull = async (roomID: number) => {
|
|||
) as any;
|
||||
let req = await select([currentCount, maxCount], null)
|
||||
.query(db);
|
||||
if(maxCount == -1) return false;
|
||||
if (maxCount == -1) return false;
|
||||
return req[0][currentCount] >= req[0][maxCount];
|
||||
};
|
||||
|
||||
|
@ -26,12 +26,25 @@ export const canCreateRoom = async (accID: number) => {
|
|||
"currentCount"
|
||||
) as any;
|
||||
let maxCount = alias(
|
||||
select([accounts.maxRooms],accounts)
|
||||
select([accounts.maxRooms], accounts)
|
||||
.where(eq(accounts.accID, accID)),
|
||||
"maxCount"
|
||||
) as any;
|
||||
let req = await select([currentCount, maxCount], null)
|
||||
.query(db);
|
||||
if(maxCount == -1) return true;
|
||||
if (maxCount == -1) return true;
|
||||
return req[0][currentCount] < req[0][maxCount];
|
||||
}
|
||||
};
|
||||
|
||||
export const isCategoryInRoom = async (roomID: number, catID: number) => {
|
||||
let res = await select([listCategories.roomID], listCategories)
|
||||
.where(eq(listCategories.listCatID, catID))
|
||||
.query(db);
|
||||
return res.length > 0 && res[0][listCategories.roomID] == roomID;
|
||||
};
|
||||
export const isProductInRoom = async (roomID: number, prodID: number) => {
|
||||
let res = await select([listProducts.roomID], listProducts)
|
||||
.where(eq(listProducts.listProdID, prodID))
|
||||
.query(db);
|
||||
return res.length > 0 && res[0][listProducts.roomID] == roomID;
|
||||
};
|
|
@ -201,8 +201,8 @@ listProducts.addAttributes({
|
|||
onUpdate: onAction.cascade
|
||||
}
|
||||
},
|
||||
title: { type: TEXT },
|
||||
description: { type: TEXT },
|
||||
title: { type: VARCHAR(256) },
|
||||
description: { type: VARCHAR(4096) },
|
||||
category: {
|
||||
type: INT,
|
||||
foreignKey: {
|
||||
|
@ -212,8 +212,8 @@ listProducts.addAttributes({
|
|||
}
|
||||
},
|
||||
defUnit: { type: SMALLINT },
|
||||
defValue: { type: TEXT },
|
||||
ean: { type: INT, notNull: false }
|
||||
defValue: { type: VARCHAR(256) },
|
||||
ean: { type: VARCHAR(64), notNull: false }
|
||||
});
|
||||
listProducts.addAttribute("parent", INT, {
|
||||
foreignKey: {
|
||||
|
|
Loading…
Reference in a new issue