Files
bodyshop/server/rr/rr-customer.js

67 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// -----------------------------------------------------------------------------
// RR Customer endpoints (create/update) wired through MakeRRCall.
// Shapes are mapped via rr-mappers.js and validated via rr-error.js.
//
// Whats still missing (complete when you wire to the PDFs):
// - Final request envelopes & field names in rr-mappers.js
// - Definitive success/error envelope checks in rr-error.js
// - Any RR-specific headers (dealer/tenant/site) once known
// -----------------------------------------------------------------------------
const { MakeRRCall, RRActions } = require("./rr-helpers");
const { assertRrOk } = require("./rr-error");
const { mapCustomerInsert, mapCustomerUpdate } = require("./rr-mappers");
/**
* Create a customer in RR.
*
* @param {Object} deps
* @param {Socket|ExpressRequest} deps.socket
* @param {Object} deps.redisHelpers - redisHelpers API (not used here directly)
* @param {Object} deps.JobData - Rome Job data used to build the payload
* @returns {Promise<any>} RR response (envelope TBD)
*/
async function RrCustomerInsert({ socket, redisHelpers, JobData }) {
// Map JobData -> RR "Customer Insert" request body
const body = mapCustomerInsert(JobData);
const data = await MakeRRCall({
...RRActions.CreateCustomer, // POST /customer/v1/
body,
redisHelpers,
socket,
jobid: JobData?.id
});
// TODO: assertRrOk should be updated once RRs success envelope is finalized
return assertRrOk(data, { apiName: "RR Create Customer" });
}
/**
* Update an existing customer in RR.
*
* @param {Object} deps
* @param {Socket|ExpressRequest} deps.socket
* @param {Object} deps.redisHelpers
* @param {Object} deps.JobData - context only (job id for correlation)
* @param {Object} deps.existingCustomer - Current RR customer record
* @param {Object} deps.patch - Minimal delta from UI to apply onto existingCustomer
* @returns {Promise<any>} RR response
*/
async function RrCustomerUpdate({ socket, redisHelpers, JobData, existingCustomer, patch }) {
// Build a merged/normalized payload for RR Update
const body = mapCustomerUpdate(existingCustomer, patch);
const data = await MakeRRCall({
...RRActions.UpdateCustomer, // PUT /customer/v1/ (append id inside body/path per final spec)
body,
redisHelpers,
socket,
jobid: JobData?.id
});
return assertRrOk(data, { apiName: "RR Update Customer" });
}
module.exports = { RrCustomerInsert, RrCustomerUpdate };