feat: revamp customer notes feature

This commit is contained in:
Sérgio Salgado 2021-12-09 19:09:58 +00:00
parent dcd3259484
commit eb8737872d
20 changed files with 678 additions and 146 deletions

View file

@ -4,27 +4,28 @@ const _ = require('lodash/fp')
const db = require('./db')
const getCustomerNotes = customerId => {
const sql = `SELECT * FROM customer_notes WHERE customer_id=$1 LIMIT 1`
const sql = `SELECT * FROM customer_notes WHERE customer_id=$1`
return db.oneOrNone(sql, [customerId]).then(res => _.mapKeys((_, key) => _.camelize(key), res))
}
const createCustomerNotes = (customerId, userId, content) => {
const sql = `INSERT INTO customer_notes (id, customer_id, last_edited_by, last_edited_at, content) VALUES ($1, $2, $3, now(), $4)`
return db.none(sql, [uuid.v4(), customerId, userId, content])
const createCustomerNote = (customerId, userId, title, content) => {
const sql = `INSERT INTO customer_notes (id, customer_id, last_edited_by, last_edited_at, title, content) VALUES ($1, $2, $3, now(), $4, $5)`
return db.none(sql, [uuid.v4(), customerId, userId, title, content])
}
const updateCustomerNotes = (customerId, userId, content) => {
const sql = `UPDATE customer_notes SET last_edited_at=now(), last_edited_by=$1, content=$2 WHERE customer_id=$3 RETURNING *`
return db.any(sql, [userId, content, customerId])
.then(res => {
if (_.isEmpty(res)) {
createCustomerNotes(customerId, userId, content)
}
})
const deleteCustomerNote = noteId => {
const sql = `DELETE FROM customer_notes WHERE id=$1`
return db.none(sql, [noteId])
}
const updateCustomerNote = (noteId, userId, content) => {
const sql = `UPDATE customer_notes SET last_edited_at=now(), last_edited_by=$1, content=$2 WHERE id=$3`
return db.none(sql, [userId, content, noteId])
}
module.exports = {
getCustomerNotes,
createCustomerNotes,
updateCustomerNotes
createCustomerNote,
deleteCustomerNote,
updateCustomerNote
}