feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration / RRScratch2 / Math Adjustments / DMS State cleaning on Page load

This commit is contained in:
Dave
2025-11-25 14:58:53 -05:00
parent 2b1836d450
commit ec29a22984
4 changed files with 103 additions and 5 deletions

View File

@@ -584,19 +584,22 @@ function applyRomeProfileAdjustments({
}
});
// Labor / materials adjustments
// Labor / materials adjustments (match CDK semantics: check `adjustment`, add `adjustments`)
Object.keys(rateMap).forEach((key) => {
const rate = rateMap[key];
if (!rate || !rate.adjustment) return;
const adjMoney = Dinero(rate.adjustment);
if (adjMoney.isZero()) return;
const checkMoney = Dinero(rate.adjustment);
if (checkMoney.isZero()) return;
const accountName = selectedDmsAllocationConfig.profits[key.toUpperCase()];
const otherAccount = bodyshop.md_responsibility_centers.profits.find((c) => c.name === accountName);
if (otherAccount) {
const bucket = ensureCenterBucket(profitCenterHash, accountName);
// Note: we intentionally use `rate.adjustments` here to mirror CDK behaviour
const adjMoney = Dinero(rate.adjustments);
bucket.extrasSale = bucket.extrasSale.add(adjMoney);
debugLog("Added rate adjustment", {

View File

@@ -67,6 +67,34 @@ const redisSocketEvents = ({ io, redisHelpers, ioHelpers, logger }) => {
// Register Socket Events
const registerSocketEvents = (socket) => {
// DMS reset events (clear per-socket DMS transactional cache)
const registerDmsResetEvents = (socket) => {
socket.on("dms-reset-context", async ({ jobId, mode } = {}, ack) => {
try {
// This clears all transactional session data for this socket
// (RR txEnvelope/JobData/SelectedCustomer/PendingRO/etc, Fortellis, etc.)
await clearSessionTransactionData(socket.id);
createLogEvent(
socket,
"debug",
`DMS reset-context: cleared transactional session data` +
(jobId ? ` (jobId=${jobId})` : "") +
(mode ? ` (mode=${mode})` : "")
);
if (typeof ack === "function") {
ack({ ok: true });
}
} catch (error) {
createLogEvent(socket, "error", `DMS reset-context failed: ${error.message}`);
if (typeof ack === "function") {
ack({ ok: false, error: error.message });
}
}
});
};
// Token Update Events
const registerUpdateEvents = (socket) => {
let latestTokenTimestamp = 0;
@@ -164,7 +192,9 @@ const redisSocketEvents = ({ io, redisHelpers, ioHelpers, logger }) => {
// Optional: clear transactional session
try {
await clearSessionTransactionData(socket.id);
} catch {}
} catch {
//
}
// Leave all rooms except the default room (socket.id)
const rooms = Array.from(socket.rooms).filter((room) => room !== socket.id);
for (const room of rooms) {
@@ -363,6 +393,7 @@ const redisSocketEvents = ({ io, redisHelpers, ioHelpers, logger }) => {
};
// Call Handlers
registerDmsResetEvents(socket);
registerRoomAndBroadcastEvents(socket);
registerUpdateEvents(socket);
registerMessagingEvents(socket);

View File

@@ -115,6 +115,33 @@ function SetLegacyWebsocketHandlers(io) {
socket.on("disconnect", () => {
createLogEvent(socket, "DEBUG", `User disconnected.`);
});
// DMS reset for legacy WS (CDK / PBS)
socket.on("dms-reset-context", ({ jobId, mode } = {}, ack) => {
try {
// Clear any per-socket DMS state that can leak across jobs
socket.selectedCustomerId = null; // CDK / PBS AR
socket.txEnvelope = null; // PBS AP export
socket.apAllocations = null; // PBS AP allocations
createLogEvent(
socket,
"DEBUG",
`DMS reset-context (legacy WS): cleared per-socket state` +
(jobId ? ` (jobId=${jobId})` : "") +
(mode ? ` (mode=${mode})` : "")
);
if (typeof ack === "function") {
ack({ ok: true });
}
} catch (error) {
createLogEvent(socket, "ERROR", `DMS reset-context (legacy WS) failed: ${error.message}`);
if (typeof ack === "function") {
ack({ ok: false, error: error.message });
}
}
});
});
}