add: brand db

This commit is contained in:
2025-10-10 14:26:12 +07:00
parent 62823524bc
commit cc4d135a53

View File

@@ -1,65 +1,66 @@
const pool = require("../config");
// Get all brands
const getAllBrandsDb = async (filters = {}) => {
const { whereConditions, queryParams } = pool.buildFilterQuery([
{ column: "b.brand_name", param: filters.brand_name, type: "string" },
{ column: "b.created_by", param: filters.created_by, type: "number" },
]);
const getAllBrandsDb = async (searchParams = {}) => {
let queryParams = [];
const whereClause = whereConditions.length ? `AND ${whereConditions.join(" AND ")}` : "";
if (searchParams.limit) {
const page = Number(searchParams.page ?? 1) - 1;
queryParams = [Number(searchParams.limit ?? 10), page];
}
const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike(
["b.brand_name"],
searchParams.criteria,
queryParams
);
queryParams = whereParamOr ? whereParamOr : queryParams;
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
[
{ column: "b.brand_name", param: searchParams.name, type: "string" },
{ column: "b.created_by", param: searchParams.created_by, type: "number" },
],
queryParams
);
queryParams = whereParamAnd ? whereParamAnd : queryParams;
const queryText = `
SELECT
b.brand_id,
b.brand_name,
b.created_at,
b.updated_at,
b.deleted_at,
b.created_by,
b.updated_by,
b.deleted_by
SELECT COUNT(*) OVER() AS total_data, b.*
FROM m_brands b
WHERE b.deleted_at IS NULL ${whereClause}
WHERE b.deleted_at IS NULL
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""}
${whereOrConditions ? whereOrConditions : ""}
ORDER BY b.brand_id ASC
${searchParams.limit ? `OFFSET $2 ROWS FETCH NEXT $1 ROWS ONLY` : ""};
`;
const result = await pool.query(queryText, queryParams);
return result.recordset;
const total = result?.recordset.length > 0 ? parseInt(result.recordset[0].total_data, 10) : 0;
return { data: result.recordset, total };
};
// Get brand by ID
const getBrandByIdDb = async (id) => {
const queryText = `
SELECT
brand_id,
brand_name,
created_at,
updated_at,
deleted_at,
created_by,
updated_by,
deleted_by
FROM m_brands
WHERE brand_id = $1 AND deleted_at IS NULL
SELECT b.*
FROM m_brands b
WHERE b.brand_id = $1 AND b.deleted_at IS NULL
`;
const result = await pool.query(queryText, [id]);
return result.recordset[0];
return result.recordset;
};
// Get brand by name
const getBrandByNameDb = async (name) => {
const queryText = `
SELECT
brand_id,
brand_name,
created_at,
updated_at,
deleted_at,
created_by,
updated_by,
deleted_by
FROM m_brands
WHERE brand_name = $1 AND deleted_at IS NULL
SELECT b.*
FROM m_brands b
WHERE b.brand_name = $1 AND b.deleted_at IS NULL
`;
const result = await pool.query(queryText, [name]);
return result.recordset[0];
@@ -67,33 +68,38 @@ const getBrandByNameDb = async (name) => {
// Create brand
const createBrandDb = async (data) => {
const { query, values } = pool.buildDynamicInsert("m_brands", {
const store = {
...data,
created_at: new Date(),
});
const result = await pool.query(query, values);
};
const { query: queryText, values } = pool.buildDynamicInsert("m_brands", store);
const result = await pool.query(queryText, values);
const insertedId = result.recordset[0]?.inserted_id;
if (!insertedId) return null;
return getBrandByIdDb(insertedId);
return insertedId ? await getBrandByIdDb(insertedId) : null;
};
// Update brand
const updateBrandDb = async (id, data) => {
const { query, values } = pool.buildDynamicUpdate(
"m_brands",
{ ...data, updated_at: new Date() },
{ brand_id: id }
);
await pool.query(query, values);
const store = {
...data,
updated_at: new Date(),
};
const whereData = {
brand_id: id,
};
const { query: queryText, values } = pool.buildDynamicUpdate("m_brands", store, whereData);
await pool.query(`${queryText} AND deleted_at IS NULL`, values);
return getBrandByIdDb(id);
};
// Soft delete brand
const softDeleteBrandDb = async (id, deletedBy) => {
const deleteBrandDb = async (id, deletedBy) => {
const queryText = `
UPDATE m_brands
SET deleted_at = GETDATE(),
deleted_by = $1
SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1
WHERE brand_id = $2 AND deleted_at IS NULL
`;
await pool.query(queryText, [deletedBy, id]);
@@ -106,5 +112,5 @@ module.exports = {
getBrandByNameDb,
createBrandDb,
updateBrandDb,
softDeleteBrandDb,
deleteBrandDb,
};