Integrate rough estimate balance
This commit is contained in:
commit
573487666c
45 changed files with 9378 additions and 58 deletions
|
|
@ -3,9 +3,12 @@ import {
|
|||
projectStateSnapshotSchemaVersion,
|
||||
type ProjectStateSnapshot,
|
||||
} from "./project-state-snapshot.model.js";
|
||||
import { parseEstimateBalanceSnapshot } from "../../estimate-balance/domain/estimate-balance-command.model.js";
|
||||
import type { EstimateBalanceSnapshot } from "../../shared/estimate-balance/estimate-balance.js";
|
||||
|
||||
export const projectTransferFormat = "leistungsbilanz.project" as const;
|
||||
export const projectTransferFormatVersion = 1 as const;
|
||||
export const projectTransferFormatVersion = 2 as const;
|
||||
const legacyProjectTransferFormatVersion = 1 as const;
|
||||
|
||||
export interface ProjectTransferEnvelope {
|
||||
format: typeof projectTransferFormat;
|
||||
|
|
@ -13,12 +16,14 @@ export interface ProjectTransferEnvelope {
|
|||
exportedAtIso: string;
|
||||
payloadSha256: string;
|
||||
projectState: ProjectStateSnapshot;
|
||||
estimateBalances: EstimateBalanceSnapshot[];
|
||||
}
|
||||
|
||||
export function createProjectTransferEnvelope(
|
||||
exportedAtIso: string,
|
||||
payloadSha256: string,
|
||||
projectState: ProjectStateSnapshot
|
||||
projectState: ProjectStateSnapshot,
|
||||
estimateBalances: EstimateBalanceSnapshot[]
|
||||
): ProjectTransferEnvelope {
|
||||
return parseProjectTransferEnvelope({
|
||||
format: projectTransferFormat,
|
||||
|
|
@ -26,6 +31,7 @@ export function createProjectTransferEnvelope(
|
|||
exportedAtIso,
|
||||
payloadSha256,
|
||||
projectState,
|
||||
estimateBalances,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -37,7 +43,8 @@ export function parseProjectTransferEnvelope(
|
|||
}
|
||||
if (
|
||||
value.format !== projectTransferFormat ||
|
||||
value.formatVersion !== projectTransferFormatVersion
|
||||
(value.formatVersion !== legacyProjectTransferFormatVersion &&
|
||||
value.formatVersion !== projectTransferFormatVersion)
|
||||
) {
|
||||
throw new Error("Project transfer format is not supported.");
|
||||
}
|
||||
|
|
@ -57,7 +64,13 @@ export function parseProjectTransferEnvelope(
|
|||
if (projectState.schemaVersion !== projectStateSnapshotSchemaVersion) {
|
||||
throw new Error("Project transfer snapshot version is not supported.");
|
||||
}
|
||||
if (Object.keys(value).length !== 5) {
|
||||
const estimateBalances =
|
||||
value.formatVersion === legacyProjectTransferFormatVersion
|
||||
? []
|
||||
: parseTransferEstimateBalances(value.estimateBalances, projectState.project.id);
|
||||
const expectedFieldCount =
|
||||
value.formatVersion === legacyProjectTransferFormatVersion ? 5 : 6;
|
||||
if (Object.keys(value).length !== expectedFieldCount) {
|
||||
throw new Error("Project transfer file contains unknown fields.");
|
||||
}
|
||||
return {
|
||||
|
|
@ -66,9 +79,43 @@ export function parseProjectTransferEnvelope(
|
|||
exportedAtIso: value.exportedAtIso,
|
||||
payloadSha256: value.payloadSha256,
|
||||
projectState,
|
||||
estimateBalances,
|
||||
};
|
||||
}
|
||||
|
||||
export function serializeProjectTransferPayload(
|
||||
projectState: ProjectStateSnapshot,
|
||||
estimateBalances: EstimateBalanceSnapshot[]
|
||||
) {
|
||||
const parsedState = parseProjectStateSnapshot(projectState);
|
||||
return JSON.stringify({
|
||||
projectState: parsedState,
|
||||
estimateBalances: parseTransferEstimateBalances(
|
||||
estimateBalances,
|
||||
parsedState.project.id
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function remapEstimateBalances(
|
||||
source: EstimateBalanceSnapshot[],
|
||||
targetProjectId: string,
|
||||
createId: () => string
|
||||
): EstimateBalanceSnapshot[] {
|
||||
return source.map((balance) =>
|
||||
parseEstimateBalanceSnapshot({
|
||||
...balance,
|
||||
id: createId(),
|
||||
projectId: targetProjectId,
|
||||
groups: balance.groups.map((group) => ({
|
||||
...group,
|
||||
id: createId(),
|
||||
rows: group.rows.map((row) => ({ ...row, id: createId() })),
|
||||
})),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export function remapProjectState(
|
||||
source: ProjectStateSnapshot,
|
||||
targetProjectId: string,
|
||||
|
|
@ -282,6 +329,37 @@ function requiredId(ids: ReadonlyMap<string, string>, sourceId: string) {
|
|||
return targetId;
|
||||
}
|
||||
|
||||
function parseTransferEstimateBalances(
|
||||
value: unknown,
|
||||
projectId: string
|
||||
): EstimateBalanceSnapshot[] {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Project transfer estimate balances must be an array.");
|
||||
}
|
||||
const balances = value.map(parseEstimateBalanceSnapshot);
|
||||
if (balances.some((balance) => balance.projectId !== projectId)) {
|
||||
throw new Error("Project transfer estimate balance belongs to another project.");
|
||||
}
|
||||
assertUniqueIds(balances.map((balance) => balance.id), "estimate balance");
|
||||
assertUniqueIds(
|
||||
balances.flatMap((balance) => balance.groups.map((group) => group.id)),
|
||||
"estimate group"
|
||||
);
|
||||
assertUniqueIds(
|
||||
balances.flatMap((balance) =>
|
||||
balance.groups.flatMap((group) => group.rows.map((row) => row.id))
|
||||
),
|
||||
"estimate row"
|
||||
);
|
||||
return balances;
|
||||
}
|
||||
|
||||
function assertUniqueIds(ids: string[], label: string) {
|
||||
if (new Set(ids).size !== ids.length) {
|
||||
throw new Error(`Project transfer contains a duplicate ${label} id.`);
|
||||
}
|
||||
}
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue