refactor backend and persist stock campaign queue
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m32s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m32s
This commit is contained in:
47
backend/services/ordersService.js
Normal file
47
backend/services/ordersService.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const { pool } = require('../db');
|
||||
const { formatOrderRow, normalizeOrderPayload } = require('../mappers/orderMapper');
|
||||
|
||||
const listOrders = async () => {
|
||||
const result = await pool.query('SELECT * FROM orders ORDER BY id DESC');
|
||||
return result.rows.map(formatOrderRow);
|
||||
};
|
||||
|
||||
const upsertOrders = async (payload) => {
|
||||
const client = await pool.connect();
|
||||
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
|
||||
const insertQuery = `
|
||||
INSERT INTO orders (
|
||||
cliente_nome, data_pedido, valor_pedido,
|
||||
produto_id, produto_descricao, quantidade, valor_unitario, pedido_id, cliente_fone
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
ON CONFLICT (pedido_id, produto_id) DO UPDATE SET
|
||||
cliente_nome = EXCLUDED.cliente_nome,
|
||||
data_pedido = EXCLUDED.data_pedido,
|
||||
valor_pedido = EXCLUDED.valor_pedido,
|
||||
produto_descricao = EXCLUDED.produto_descricao,
|
||||
quantidade = EXCLUDED.quantidade,
|
||||
valor_unitario = EXCLUDED.valor_unitario,
|
||||
cliente_fone = EXCLUDED.cliente_fone,
|
||||
created_at = CURRENT_TIMESTAMP
|
||||
`;
|
||||
|
||||
for (const item of payload) {
|
||||
await client.query(insertQuery, normalizeOrderPayload(item));
|
||||
}
|
||||
|
||||
await client.query('COMMIT');
|
||||
} catch (error) {
|
||||
await client.query('ROLLBACK');
|
||||
throw error;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
listOrders,
|
||||
upsertOrders
|
||||
};
|
||||
Reference in New Issue
Block a user