refactor: isolate operation ranges and intents
This commit is contained in:
+43
-46
@@ -392,52 +392,15 @@ export class BrokerageOperationsRepository {
|
||||
const work = await tx.tbankSyncWork.update({
|
||||
where: { id: previous.id },
|
||||
data: {
|
||||
intent: {
|
||||
atomic: previousIntent?.atomic ?? publicationAtomic,
|
||||
atomicIntervals: previousIntent?.atomic
|
||||
? [
|
||||
...new Set([
|
||||
...(previousIntent.atomicIntervals ??
|
||||
previousIntent.intervals),
|
||||
...additions.map(intervalKey),
|
||||
]),
|
||||
]
|
||||
: [],
|
||||
screenIntervals: [
|
||||
...new Set([
|
||||
...(previousIntent?.screenIntervals ??
|
||||
(previous.priority === operationsSyncPriority.screen
|
||||
? (previousIntent?.intervals ?? [])
|
||||
: [])),
|
||||
...(priority === operationsSyncPriority.screen
|
||||
? [...pending, ...additions]
|
||||
.filter((item) =>
|
||||
intervals.some((target) =>
|
||||
operationIntervalsOverlap(item, target),
|
||||
),
|
||||
)
|
||||
.map(intervalKey)
|
||||
: []),
|
||||
]),
|
||||
],
|
||||
intervals: (priority < previous.priority
|
||||
? [
|
||||
...pending.filter((item) =>
|
||||
intervals.some((target) =>
|
||||
operationIntervalsOverlap(item, target),
|
||||
),
|
||||
),
|
||||
...additions,
|
||||
...pending.filter(
|
||||
(item) =>
|
||||
!intervals.some((target) =>
|
||||
operationIntervalsOverlap(item, target),
|
||||
),
|
||||
),
|
||||
]
|
||||
: [...pending, ...additions]
|
||||
).map(intervalKey),
|
||||
},
|
||||
intent: extendRunningIntent({
|
||||
previous: previousIntent,
|
||||
publicationAtomic,
|
||||
pending,
|
||||
additions,
|
||||
intervals,
|
||||
priority,
|
||||
previousPriority: previous.priority,
|
||||
}),
|
||||
priority: Math.min(priority, previous.priority),
|
||||
},
|
||||
});
|
||||
@@ -919,6 +882,40 @@ export class BrokerageOperationsRepository {
|
||||
}
|
||||
}
|
||||
|
||||
function extendRunningIntent(input: {
|
||||
additions: OperationInterval[];
|
||||
intervals: OperationInterval[];
|
||||
pending: OperationInterval[];
|
||||
previous: OperationsIntent | undefined;
|
||||
previousPriority: number;
|
||||
priority: number;
|
||||
publicationAtomic: boolean;
|
||||
}): OperationsIntent {
|
||||
const { previous, pending, additions, intervals, priority, previousPriority, publicationAtomic } = input;
|
||||
const overlapsRequested = (item: OperationInterval) =>
|
||||
intervals.some((target) => operationIntervalsOverlap(item, target));
|
||||
const retainedScreen = previous?.screenIntervals ??
|
||||
(previousPriority === operationsSyncPriority.screen ? (previous?.intervals ?? []) : []);
|
||||
const addedScreen = priority === operationsSyncPriority.screen
|
||||
? [...pending, ...additions].filter(overlapsRequested).map(intervalKey)
|
||||
: [];
|
||||
const ordered = priority < previousPriority
|
||||
? [
|
||||
...pending.filter(overlapsRequested),
|
||||
...additions,
|
||||
...pending.filter((item) => !overlapsRequested(item)),
|
||||
]
|
||||
: [...pending, ...additions];
|
||||
return {
|
||||
atomic: previous?.atomic ?? publicationAtomic,
|
||||
atomicIntervals: previous?.atomic
|
||||
? [...new Set([...(previous.atomicIntervals ?? previous.intervals), ...additions.map(intervalKey)])]
|
||||
: [],
|
||||
screenIntervals: [...new Set([...retainedScreen, ...addedScreen])],
|
||||
intervals: ordered.map(intervalKey),
|
||||
};
|
||||
}
|
||||
|
||||
function intervalKey(interval: OperationInterval): string {
|
||||
// eslint-disable-next-line sonarjs/no-inverted-boolean-check -- Negation also rejects Invalid Date; >= would silently accept NaN.
|
||||
if (!(interval.from < interval.to)) {
|
||||
|
||||
Reference in New Issue
Block a user