a lot of bugfixes, completed abstract products and products

This commit is contained in:
2024-10-27 04:45:13 +03:00
parent a27ce5762c
commit 75f9fb6d7d
24 changed files with 462 additions and 284 deletions

View File

@@ -1,9 +1,67 @@
import db from '../db.js';
import statuses from '../utils/status.js';
import errorHandler from '../utils/pgerrorhandler.js';
class AbstractProductService {
async create (groupid, barcode, name, net_weight, image_filename, category, unit) {
await db.query("INSERT INTO abstract_products (group_id, barcode, name, net_weight, image_filename, category, unit) VALUES ($1, $2, $3, $4, $5, $6, $7)", [groupid, barcode, name, net_weight, image_filename, category, unit]);
async create(groupid, localid, barcode, name, net_weight, image_filename, category, unit) {
await db.query("INSERT INTO abstract_products (group_id, local_id, barcode, name, net_weight, image_filename, category, unit) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", [groupid, localid, barcode, name, net_weight, image_filename, category, unit])
.catch((e) => {
errorHandler(e, "abstract product")
});
}
async updateBarcode(groupId, localId, barcode) {
await db.query("UPDATE abstract_products SET barcode = $1 WHERE group_id = $2 AND local_id = $3", [barcode, groupId, localId])
.catch((e) => {
errorHandler(e, "barcode")
});
}
async updateName(groupId, localId, name) {
await db.query("UPDATE abstract_products SET name = $1 WHERE group_id = $2 AND local_id = $3", [name, groupId, localId])
.catch((e) => {
errorHandler(e, "name")
});
}
async updateNetWeight(groupId, localId, net_weight) {
await db.query("UPDATE abstract_products SET net_weight = $1 WHERE group_id = $2 AND local_id = $3", [net_weight, groupId, localId]
.catch((e) => {
errorHandler(e, "net weight")
}));
}
async updateImageFilename(groupId, localId, image_filename) {
await db.query("UPDATE abstract_products SET image_filename = $1 WHERE group_id = $2 AND local_id = $3", [image_filename, groupId, localId])
.catch((e) => {
errorHandler(e, "image filename")
});
}
async updateCategory(groupId, localId, category) {
await db.query("UPDATE abstract_products SET category = $1 WHERE group_id = $2 AND local_id = $3", [category, groupId, localId])
.catch((e) => {
errorHandler(e, "category")
});
}
async updateUnit(groupId, localId, unit) {
await db.query("UPDATE abstract_products SET unit = $1 WHERE group_id = $2 AND local_id = $3", [unit, groupId, localId])
.catch((e) => {
errorHandler(e, "unit")
});
}
async getAll(groupId) {
let result = (await db.query("SELECT local_id, barcode, name, net_weight, image_filename, category, unit FROM abstract_products WHERE group_id = $1", [groupId])).rows
if (!result) return statuses.not_found;
return result
}
async exists(groupId, localId) {
let result = (await db.query("SELECT * FROM abstract_products WHERE group_id = $1 AND local_id = $2", [groupId, localId])).rowCount;
if (!result) return false;
return true;
}
};

View File

@@ -1,13 +1,10 @@
import db from '../db.js';
import errorHandler from '../utils/pgerrorhandler.js';
import status from '../utils/status.js';
class GroupService {
async create(name, creatorId) {
let res = await db.query("INSERT INTO groups (name, admin_id) VALUES ($1, $2) RETURNING ID", [name, creatorId]).catch (e => {
if (e.code == 23505) { // already exists
return status.duplicate;
}
})
let res = await db.query("INSERT INTO groups (name, admin_id) VALUES ($1, $2) RETURNING ID", [name, creatorId]).catch(errorHandler)
return res.rows[0];
}

55
src/services/product.js Normal file
View File

@@ -0,0 +1,55 @@
import db from '../db.js';
import statuses from '../utils/status.js';
import errorHandler from '../utils/pgerrorhandler.js';
class ProductService {
async create(groupid, localid, abstract_product_id, amount, date_of_production, expiry_date) {
await db.query("INSERT INTO products (group_id, local_id, abstract_product_id, amount, date_of_production, expiry_date) VALUES ($1, $2, $3, $4, $5, $6)", [groupid, localid, abstract_product_id, amount, date_of_production, expiry_date])
.catch((e) => {
errorHandler(e, "Abstract Product")
})
}
async updateAbstractProductId(groupId, localId, abstract_product_id) {
await db.query("UPDATE products SET abstract_product_id = $1 WHERE group_id = $2 AND local_id = $3", [abstract_product_id, groupId, localId])
.catch((e) => {
errorHandler(e, "abstract product id")
});
}
async updateAmount(groupId, localId, amount) {
await db.query("UPDATE products SET amount = $1 WHERE group_id = $2 AND local_id = $3", [amount, groupId, localId])
.catch((e) => {
errorHandler(e, "amount")
});
}
async updateDateOfProduction(groupId, localId, date_of_production) {
await db.query("UPDATE products SET date_of_production = $1 WHERE group_id = $2 AND local_id = $3", [date_of_production, groupId, localId])
.catch((e) => {
errorHandler(e, "date of production")
})
}
async updateExpiryDate(groupId, localId, expiry_date) {
await db.query("UPDATE products SET expiry_date = $1 WHERE group_id = $2 AND local_id = $3", [expiry_date, groupId, localId])
.catch((e) => {
errorHandler(e, "expiry date")
})
}
async getAll(groupId) {
let result = (await db.query("SELECT local_id, abstract_product_id, amount, date_of_production, expiry_date FROM products WHERE group_id = $1", [groupId])).rows
if (!result) return statuses.not_found;
return result
}
async exists(groupId, localId) {
let result = (await db.query("SELECT * FROM products WHERE group_id = $1 AND local_id = $2", [groupId, localId])).rowCount;
if (!result) return false;
return true;
}
};
export default new ProductService();

View File

@@ -3,11 +3,9 @@ import statuses from '../utils/status.js';
import bcrypt from 'bcrypt';
class UserService {
async create (username, password) {
await db.query("INSERT INTO users (username, password) VALUES ($1, $2)", [username, bcrypt.hashSync(password, 12)]).catch (e => {
if (e.code == 23505) { // already exists
return statuses.duplicate
}
async create(username, password) {
await db.query("INSERT INTO users (username, password) VALUES ($1, $2)", [username, bcrypt.hashSync(password, 12)]).catch((e) => {
errorHandler(e, "user")
})
return statuses.ok
}