add: crud brand

This commit is contained in:
2025-10-24 11:09:30 +07:00
parent 3a95cdf315
commit 893f177abd
7 changed files with 669 additions and 42 deletions

View File

@@ -4,23 +4,28 @@ const pool = require("../config");
const getAllBrandsDb = async (searchParams = {}) => {
let queryParams = [];
// Pagination
if (searchParams.limit) {
const page = Number(searchParams.page ?? 1) - 1;
queryParams = [Number(searchParams.limit ?? 10), page];
}
// Search
const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike(
["b.brand_name"],
["a.brand_name", "a.brand_type", "a.brand_manufacture", "a.brand_model", "a.brand_code"],
searchParams.criteria,
queryParams
);
queryParams = whereParamOr ? whereParamOr : queryParams;
// Filter
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
[
{ column: "b.brand_name", param: searchParams.name, type: "string" },
{ column: "b.created_by", param: searchParams.created_by, type: "number" },
{ column: "a.brand_type", param: searchParams.type, type: "string" },
{ column: "a.brand_manufacture", param: searchParams.manufacture, type: "string" },
{ column: "a.brand_model", param: searchParams.model, type: "string" },
{ column: "a.is_active", param: searchParams.status, type: "string" },
],
queryParams
);
@@ -28,49 +33,57 @@ const getAllBrandsDb = async (searchParams = {}) => {
queryParams = whereParamAnd ? whereParamAnd : queryParams;
const queryText = `
SELECT COUNT(*) OVER() AS total_data, b.*
FROM m_brands b
WHERE b.deleted_at IS NULL
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""}
${whereOrConditions ? whereOrConditions : ""}
ORDER BY b.brand_id ASC
SELECT
COUNT(*) OVER() AS total_data,
a.*
FROM m_brands a
WHERE a.deleted_at IS NULL
${whereConditions.length > 0 ? `AND ${whereConditions.join(' AND ')}` : ''}
${whereOrConditions ? whereOrConditions : ''}
ORDER BY a.brand_id ASC
${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ''}
`;
const result = await pool.query(queryText, queryParams);
const total = result?.recordset.length > 0 ? parseInt(result.recordset[0].total_data, 10) : 0;
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) => {
// Get brand by name (path-based)
const getBrandByNameDb = async (brandName) => {
const queryText = `
SELECT b.*
FROM m_brands b
WHERE b.brand_id = $1 AND b.deleted_at IS NULL
SELECT
a.*
FROM m_brands a
WHERE a.brand_name = $1 AND a.deleted_at IS NULL
`;
const result = await pool.query(queryText, [id]);
return result.recordset;
const result = await pool.query(queryText, [brandName]);
return result.recordset[0];
};
// Get brand by name
const getBrandByNameDb = async (name) => {
// Get brand by ID (for internal use)
const getBrandByIdDb = async (id) => {
const queryText = `
SELECT b.*
FROM m_brands b
WHERE b.brand_name = $1 AND b.deleted_at IS NULL
SELECT
a.*
FROM m_brands a
WHERE a.brand_id = $1 AND a.deleted_at IS NULL
`;
const result = await pool.query(queryText, [name]);
const result = await pool.query(queryText, [id]);
return result.recordset[0];
};
// Create brand
const createBrandDb = async (data) => {
const newCode = await pool.generateKode("BRD", "m_brands", "brand_code");
const store = {
...data,
created_at: new Date(),
brand_code: newCode,
};
const { query: queryText, values } = pool.buildDynamicInsert("m_brands", store);
@@ -79,38 +92,80 @@ const createBrandDb = async (data) => {
return insertedId ? await getBrandByIdDb(insertedId) : null;
};
// Update brand
const updateBrandDb = async (id, data) => {
const store = {
...data,
updated_at: new Date(),
};
const whereData = {
brand_id: id,
};
// Update brand by name
const updateBrandDb = async (brandName, data) => {
const store = { ...data };
const whereData = { brand_name: brandName };
const { query: queryText, values } = pool.buildDynamicUpdate("m_brands", store, whereData);
await pool.query(`${queryText} AND deleted_at IS NULL`, values);
return getBrandByIdDb(id);
return getBrandByNameDb(brandName);
};
// Soft delete brand
const deleteBrandDb = async (id, deletedBy) => {
// Soft delete brand by name
const deleteBrandDb = async (brandName, deletedBy) => {
const queryText = `
UPDATE m_brands
SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1
WHERE brand_id = $2 AND deleted_at IS NULL
WHERE brand_name = $2 AND deleted_at IS NULL
`;
await pool.query(queryText, [deletedBy, id]);
await pool.query(queryText, [deletedBy, brandName]);
return true;
};
// Check if brand name exists (for validation)
const checkBrandNameExistsDb = async (brandName, excludeId = null) => {
let queryText = `
SELECT brand_id
FROM m_brands
WHERE brand_name = $1 AND deleted_at IS NULL
`;
let values = [brandName];
if (excludeId) {
queryText += ` AND brand_id != $2`;
values.push(excludeId);
}
const result = await pool.query(queryText, values);
return result.recordset.length > 0;
};
// Get brand with error codes count
const getBrandsWithErrorCodeCountDb = async (searchParams = {}) => {
let queryParams = [];
const queryText = `
SELECT
a.brand_id,
a.brand_name,
a.brand_type,
a.brand_manufacture,
a.brand_model,
a.brand_code,
a.is_active,
a.created_at,
COUNT(bc.error_code_id) as error_code_count
FROM m_brands a
LEFT JOIN brand_code bc ON a.brand_id = bc.brand_id AND bc.deleted_at IS NULL
WHERE a.deleted_at IS NULL
GROUP BY
a.brand_id, a.brand_name, a.brand_type, a.brand_manufacture,
a.brand_model, a.brand_code, a.is_active, a.created_at
ORDER BY a.brand_name
`;
const result = await pool.query(queryText, queryParams);
return result.recordset;
};
module.exports = {
getAllBrandsDb,
getBrandByIdDb,
getBrandByNameDb,
getBrandByIdDb,
createBrandDb,
updateBrandDb,
deleteBrandDb,
};
checkBrandNameExistsDb,
getBrandsWithErrorCodeCountDb,
};

94
db/brand_code.db.js Normal file
View File

@@ -0,0 +1,94 @@
const pool = require("../config");
// Get error codes by brand ID
const getErrorCodesByBrandIdDb = async (brandId) => {
const queryText = `
SELECT
a.*
FROM brand_code a
WHERE a.brand_id = $1 AND a.deleted_at IS NULL
ORDER BY a.error_code_id
`;
const result = await pool.query(queryText, [brandId]);
return result.recordset;
};
// Get error code by brand ID and error code
const getErrorCodeByBrandIdAndCodeDb = async (brandId, errorCode) => {
const queryText = `
SELECT
a.*
FROM brand_code a
WHERE a.brand_id = $1 AND a.error_code = $2 AND a.deleted_at IS NULL
`;
const result = await pool.query(queryText, [brandId, errorCode]);
return result.recordset[0];
};
// Create error code for brand
const createErrorCodeDb = async (brandId, data) => {
const store = {
brand_id: brandId,
error_code: data.error_code,
error_code_name: data.error_code_name,
error_code_description: data.error_code_description,
is_active: data.is_active,
created_by: data.created_by
};
const { query: queryText, values } = pool.buildDynamicInsert("brand_code", store);
const result = await pool.query(queryText, values);
const insertedId = result.recordset[0]?.inserted_id;
return insertedId;
};
// Update error code by brand ID and error code
const updateErrorCodeDb = async (brandId, errorCode, data) => {
const store = { ...data };
const whereData = {
brand_id: brandId,
error_code: errorCode
};
const { query: queryText, values } = pool.buildDynamicUpdate("brand_code", store, whereData);
await pool.query(`${queryText} AND deleted_at IS NULL`, values);
return true;
};
// Soft delete error code by brand ID and error code
const deleteErrorCodeDb = async (brandId, errorCode, deletedBy) => {
const queryText = `
UPDATE brand_code
SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1
WHERE brand_id = $2 AND error_code = $3 AND deleted_at IS NULL
`;
await pool.query(queryText, [deletedBy, brandId, errorCode]);
return true;
};
// Check if error code exists for brand
const checkErrorCodeExistsDb = async (brandId, errorCode, excludeId = null) => {
let queryText = `
SELECT error_code_id
FROM brand_code
WHERE brand_id = $1 AND error_code = $2 AND deleted_at IS NULL
`;
let values = [brandId, errorCode];
if (excludeId) {
queryText += ` AND error_code_id != $3`;
values.push(excludeId);
}
const result = await pool.query(queryText, values);
return result.recordset.length > 0;
};
module.exports = {
getErrorCodesByBrandIdDb,
getErrorCodeByBrandIdAndCodeDb,
createErrorCodeDb,
updateErrorCodeDb,
deleteErrorCodeDb,
checkErrorCodeExistsDb,
};

View File

@@ -0,0 +1,73 @@
const pool = require("../config");
// Get solutions by error code ID
const getSolutionsByErrorCodeIdDb = async (errorCodeId) => {
const queryText = `
SELECT
a.*
FROM brand_code_solution a
WHERE a.error_code_id = $1 AND a.deleted_at IS NULL
ORDER BY a.brand_code_solution_id
`;
const result = await pool.query(queryText, [errorCodeId]);
return result.recordset;
};
// Create solution for error code
const createSolutionDb = async (errorCodeId, data) => {
const store = {
error_code_id: errorCodeId,
solution_name: data.solution_name,
type_solution: data.type_solution,
text_solution: data.text_solution,
path_solution: data.path_solution,
is_active: data.is_active,
created_by: data.created_by
};
const { query: queryText, values } = pool.buildDynamicInsert("brand_code_solution", store);
const result = await pool.query(queryText, values);
const insertedId = result.recordset[0]?.inserted_id;
return insertedId;
};
// Update solution
const updateSolutionDb = async (solutionId, data) => {
const store = { ...data };
const whereData = { brand_code_solution_id: solutionId };
const { query: queryText, values } = pool.buildDynamicUpdate("brand_code_solution", store, whereData);
await pool.query(`${queryText} AND deleted_at IS NULL`, values);
return true;
};
// Soft delete solution
const deleteSolutionDb = async (solutionId, deletedBy) => {
const queryText = `
UPDATE brand_code_solution
SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1
WHERE brand_code_solution_id = $2 AND deleted_at IS NULL
`;
await pool.query(queryText, [deletedBy, solutionId]);
return true;
};
// Get solution by ID
const getSolutionByIdDb = async (solutionId) => {
const queryText = `
SELECT
a.*
FROM brand_code_solution a
WHERE a.brand_code_solution_id = $1 AND a.deleted_at IS NULL
`;
const result = await pool.query(queryText, [solutionId]);
return result.recordset[0];
};
module.exports = {
getSolutionsByErrorCodeIdDb,
createSolutionDb,
updateSolutionDb,
deleteSolutionDb,
getSolutionByIdDb,
};