Skip to content

Commit cd17266

Browse files
committed
reduce code duplications
1 parent a6d268f commit cd17266

3 files changed

Lines changed: 71 additions & 75 deletions

File tree

lib/utils/HoldingsAbandonmentAnalyzer/strategies/BaseStrategy.js

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,40 +53,83 @@ export class HoldingsAbandonmentBaseStrategy {
5353

5454
/**
5555
* Helper to build standardized result object for a holding
56-
* Handles tenantId extraction, explain mode, and result structure
56+
* Handles abandonment logic, tenantId extraction, explain mode, and result structure
5757
*
5858
* @param {Object} params - Parameters for building result
59-
* @param {boolean} params.abandoned - Whether the holding is abandoned
59+
* @param {string} [params.actionType] - Optional action type for the operation
6060
* @param {boolean} params.explain - Whether to include detailed explanation
61-
* @param {Object} [params.explainData] - Optional explain data (cleared, remaining, related, actionType)
6261
* @param {string} params.holdingId - The holding ID
6362
* @param {Array} params.items - All items in the holding
63+
* @param {Array} params.itemsToClear - Items to be cleared
6464
* @param {Array} params.pieces - All pieces in the holding
65+
* @param {Array} params.piecesToClear - Pieces to be cleared
66+
* @param {Array} params.poLines - All PO lines in the holding
67+
* @param {Array} params.poLinesToClear - PO lines to be cleared
68+
* @param {Array} params.remainingItems - Remaining items
69+
* @param {Array} params.remainingPieces - Remaining pieces
70+
* @param {Array} params.remainingPoLines - Remaining PO lines
6571
* @returns {Object} Standardized result object
6672
*/
6773
static buildResult({
68-
abandoned,
74+
actionType,
6975
explain,
70-
explainData,
7176
holdingId,
7277
items,
78+
itemsToClear,
7379
pieces,
80+
piecesToClear,
81+
poLines,
82+
poLinesToClear,
83+
remainingItems,
84+
remainingPieces,
85+
remainingPoLines,
7486
}) {
87+
// Determine if holding is abandoned
88+
const abandoned = (
89+
remainingPieces.length === 0 &&
90+
remainingItems.length === 0 &&
91+
remainingPoLines.length === 0
92+
);
93+
7594
// Extract tenantId from pieces or items (optional field)
7695
// Try pieces first, then items
77-
const tenantId = pieces.find((p) => p.tenantId)?.tenantId
78-
|| items.find((i) => i.tenantId)?.tenantId;
96+
const tenantId = (
97+
pieces.find((p) => p.tenantId)?.tenantId
98+
|| items.find((i) => i.tenantId)?.tenantId
99+
);
79100

80101
const result = {
81-
id: holdingId,
82102
abandoned,
103+
id: holdingId,
83104
};
84105

85106
if (tenantId) {
86107
result.tenantId = tenantId;
87108
}
88109

89-
if (explain && explainData) {
110+
if (explain) {
111+
const explainData = {
112+
cleared: {
113+
items: itemsToClear.map((i) => i.id),
114+
pieces: piecesToClear.map((p) => p.id),
115+
poLines: poLinesToClear.map((pl) => pl.id),
116+
},
117+
related: {
118+
items: items.map((i) => i.id),
119+
pieces: pieces.map((p) => p.id),
120+
poLines: poLines.map((pl) => pl.id),
121+
},
122+
remaining: {
123+
items: remainingItems.map((i) => i.id),
124+
pieces: remainingPieces.map((p) => p.id),
125+
poLines: remainingPoLines.map((pl) => pl.id),
126+
},
127+
};
128+
129+
if (actionType) {
130+
explainData.actionType = actionType;
131+
}
132+
90133
result.explain = explainData;
91134
}
92135

lib/utils/HoldingsAbandonmentAnalyzer/strategies/PieceStrategy.js

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -149,41 +149,18 @@ export class HoldingsAbandonmentPieceStrategy extends HoldingsAbandonmentBaseStr
149149

150150
const remainingPoLines = baseRemainingPoLines.concat(incomingPoLines);
151151

152-
const abandoned = (
153-
remainingPieces.length === 0 &&
154-
remainingItems.length === 0 &&
155-
remainingPoLines.length === 0
156-
);
157-
158-
const explainData = (
159-
explain
160-
? {
161-
cleared: {
162-
items: items.filter((i) => closure.items.has(i.id)).map((i) => i.id),
163-
pieces: piecesToClear.map((p) => p.id),
164-
poLines: Array.from(poLinesToClearSet),
165-
},
166-
related: {
167-
items: items.map((i) => i.id),
168-
pieces: pieces.map((p) => p.id),
169-
poLines: poLines.map((pl) => pl.id),
170-
},
171-
remaining: {
172-
items: remainingItems.map((i) => i.id),
173-
pieces: remainingPieces.map((p) => p.id),
174-
poLines: remainingPoLines.map((pl) => pl.id),
175-
},
176-
}
177-
: undefined
178-
);
179-
180152
return this.buildResult({
153+
explain,
181154
holdingId,
182-
abandoned,
183-
pieces,
184155
items,
185-
explain,
186-
explainData,
156+
itemsToClear: items.filter((i) => closure.items.has(i.id)),
157+
pieces,
158+
piecesToClear,
159+
poLines,
160+
poLinesToClear: Array.from(poLinesToClearSet).map((id) => ({ id })),
161+
remainingItems,
162+
remainingPieces,
163+
remainingPoLines,
187164
});
188165
});
189166
}

lib/utils/HoldingsAbandonmentAnalyzer/strategies/PoLineStrategy.js

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -106,43 +106,19 @@ export class HoldingsAbandonmentPOLineStrategy extends HoldingsAbandonmentBaseSt
106106
const remainingItems = items.filter((i) => !itemsToClear.some((ci) => ci.id === i.id));
107107
const remainingPoLines = poLines.filter((pl) => !closure.poLines.has(pl.id));
108108

109-
// Holding is abandoned if nothing remains
110-
const abandoned = (
111-
remainingPieces.length === 0 &&
112-
remainingItems.length === 0 &&
113-
remainingPoLines.length === 0
114-
);
115-
116-
const explainData = (
117-
explain
118-
? {
119-
actionType,
120-
cleared: {
121-
items: itemsToClear.map((i) => i.id),
122-
pieces: piecesToClear.map((p) => p.id),
123-
poLines: poLinesToClear.map((pl) => pl.id),
124-
},
125-
related: {
126-
items: items.map((i) => i.id),
127-
pieces: pieces.map((p) => p.id),
128-
poLines: poLines.map((pl) => pl.id),
129-
},
130-
remaining: {
131-
items: remainingItems.map((i) => i.id),
132-
pieces: remainingPieces.map((p) => p.id),
133-
poLines: remainingPoLines.map((pl) => pl.id),
134-
},
135-
}
136-
: undefined
137-
);
138-
139109
return this.buildResult({
110+
actionType,
111+
explain,
140112
holdingId,
141-
abandoned,
142-
pieces,
143113
items,
144-
explain,
145-
explainData,
114+
itemsToClear,
115+
pieces,
116+
piecesToClear,
117+
poLines,
118+
poLinesToClear,
119+
remainingItems,
120+
remainingPieces,
121+
remainingPoLines,
146122
});
147123
});
148124
}

0 commit comments

Comments
 (0)