refactor: change backend to typescript and update types and variables accordingly
This commit is contained in:
@@ -1,65 +1,87 @@
|
||||
import mysql from "mysql2";
|
||||
import mysql, {type ResultSetHeader, type RowDataPacket} from "mysql2/promise";
|
||||
import dotenv from "dotenv";
|
||||
import {STORAGE_ERROR_CODE} from "@stockhome/shared";
|
||||
import {returnErrorCode} from "../../../services/helperFuncs.js";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const pool = mysql
|
||||
.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.DB_NAME,
|
||||
})
|
||||
.promise();
|
||||
const pool = mysql.createPool({
|
||||
host: process.env.DB_HOST!,
|
||||
user: process.env.DB_USER!,
|
||||
password: process.env.DB_PASSWORD!,
|
||||
database: process.env.DB_NAME!,
|
||||
});
|
||||
|
||||
export const allStorages = async () => {
|
||||
const [result] = await pool.query(
|
||||
"SELECT BIN_TO_UUID(uuid) AS uuid, name, description, created_at, updated_at FROM storage_locations;",
|
||||
);
|
||||
const [result] = await pool.query<RowDataPacket[]>(
|
||||
"SELECT BIN_TO_UUID(uuid) AS uuid, name, description, created_at, updated_at FROM storage_locations;",
|
||||
);
|
||||
|
||||
if (result.length > 0) {
|
||||
return { code: "ss001", data: result };
|
||||
}
|
||||
if (result.length > 0) {
|
||||
return {
|
||||
success: true,
|
||||
code: "SS001",
|
||||
data: result,
|
||||
message: "Successfully fetched all storage locations.",
|
||||
};
|
||||
}
|
||||
|
||||
return returnErrorCode(STORAGE_ERROR_CODE.NO_STORAGE_LOCATIONS_FOUND);
|
||||
return returnErrorCode(STORAGE_ERROR_CODE.NO_STORAGE_LOCATIONS_FOUND);
|
||||
};
|
||||
|
||||
export const newStorage = async (name, description) => {
|
||||
const [result] = await pool.query(
|
||||
"INSERT INTO storage_locations (name, description) VALUES (?, ?)",
|
||||
[name, description],
|
||||
);
|
||||
export const newStorage = async (name: string, description: string) => {
|
||||
const [result] = await pool.query<ResultSetHeader>(
|
||||
"INSERT INTO storage_locations (name, description) VALUES (?, ?)",
|
||||
[name, description],
|
||||
);
|
||||
|
||||
if (result.affectedRows > 0) {
|
||||
return { code: "ss002" };
|
||||
}
|
||||
if (result.affectedRows > 0) {
|
||||
return {
|
||||
success: true,
|
||||
code: "SS002",
|
||||
data: null,
|
||||
message: "Successfully created new storage location.",
|
||||
};
|
||||
}
|
||||
|
||||
return returnErrorCode(STORAGE_ERROR_CODE.STORAGE_NOT_CREATED);
|
||||
return returnErrorCode(STORAGE_ERROR_CODE.STORAGE_NOT_CREATED);
|
||||
};
|
||||
|
||||
export const updateStorage = async (uuid, values) => {
|
||||
const [result] = await pool.query(
|
||||
"UPDATE storage_locations SET name = ?, description = ? WHERE uuid = UUID_TO_BIN(?);",
|
||||
[values.name, values.description, uuid],
|
||||
);
|
||||
export const updateStorage = async (
|
||||
uuid: string,
|
||||
values: { name: string; description: string },
|
||||
) => {
|
||||
const [result] = await pool.query<ResultSetHeader>(
|
||||
"UPDATE storage_locations SET name = ?, description = ? WHERE uuid = UUID_TO_BIN(?);",
|
||||
[values.name, values.description, uuid],
|
||||
);
|
||||
|
||||
if (result.affectedRows > 0) {
|
||||
return { code: "ss003" };
|
||||
}
|
||||
if (result.affectedRows > 0) {
|
||||
return {
|
||||
success: true,
|
||||
code: "SS003",
|
||||
data: null,
|
||||
message: "Successfully updated storage location.",
|
||||
};
|
||||
}
|
||||
|
||||
return returnErrorCode(STORAGE_ERROR_CODE.STORAGE_NOT_UPDATED);
|
||||
return returnErrorCode(STORAGE_ERROR_CODE.STORAGE_NOT_UPDATED);
|
||||
};
|
||||
|
||||
export const deleteStorage = async (uuid) => {
|
||||
const [result] = await pool.query(
|
||||
"DELETE FROM storage_locations WHERE uuid = UUID_TO_BIN(?);",
|
||||
[uuid],
|
||||
);
|
||||
export const deleteStorage = async (uuid: string) => {
|
||||
const [result] = await pool.query<ResultSetHeader>(
|
||||
"DELETE FROM storage_locations WHERE uuid = UUID_TO_BIN(?);",
|
||||
[uuid],
|
||||
);
|
||||
|
||||
if (result.affectedRows > 0) {
|
||||
return { code: "ss004" };
|
||||
}
|
||||
if (result.affectedRows > 0) {
|
||||
return {
|
||||
success: true,
|
||||
code: "SS004",
|
||||
data: null,
|
||||
message: "Successfully deleted storage location.",
|
||||
};
|
||||
}
|
||||
|
||||
return returnErrorCode(STORAGE_ERROR_CODE.STORAGE_NOT_DELETED);
|
||||
};
|
||||
return returnErrorCode(STORAGE_ERROR_CODE.STORAGE_NOT_DELETED);
|
||||
};
|
||||
Reference in New Issue
Block a user