fix config notice display
This commit is contained in:
+124
-82
@@ -393,6 +393,74 @@ function createApp({
|
||||
return removed;
|
||||
}
|
||||
|
||||
async function removeProductionLineData(database, { companyId, productionLineId, failOnActiveLicense = true }) {
|
||||
const line = database.productionLines.find((item) => item.id === productionLineId && item.companyId === companyId);
|
||||
if (!line) return { success: false, errMsg: "产线不存在" };
|
||||
|
||||
const activeLicenses = database.licenses.filter((item) =>
|
||||
item.companyId === companyId && item.productionLineId === productionLineId && item.status === "active"
|
||||
);
|
||||
if (failOnActiveLicense && activeLicenses.length) {
|
||||
return {
|
||||
success: false,
|
||||
errMsg: `请先撤销该产线的 ${activeLicenses.length} 个有效许可证后再删除产线`,
|
||||
errCode: "ACTIVE_LICENSES_PRESENT"
|
||||
};
|
||||
}
|
||||
|
||||
const deviceId = line.deviceId;
|
||||
const relatedFileIDs = database.fileRecords
|
||||
.filter((record) => {
|
||||
if (record.fileID.startsWith(`model://${deviceId}/`)) return true;
|
||||
const prefix = `${deviceId}/`;
|
||||
return record.folder === deviceId || record.folder.startsWith(prefix);
|
||||
})
|
||||
.map((record) => record.fileID);
|
||||
|
||||
let deletedFileCount = 0;
|
||||
for (const fileID of new Set(relatedFileIDs)) {
|
||||
deletedFileCount += await removeFile(database, fileID);
|
||||
}
|
||||
|
||||
const beforeFeedback = database.identificationFeedback.length;
|
||||
database.identificationFeedback = database.identificationFeedback.filter((item) => item.deviceId !== deviceId);
|
||||
const deletedFeedback = beforeFeedback - database.identificationFeedback.length;
|
||||
|
||||
const beforeRequests = database.volumeConfigRequests.length;
|
||||
database.volumeConfigRequests = database.volumeConfigRequests.filter((item) => item.deviceId !== deviceId);
|
||||
const deletedRequests = beforeRequests - database.volumeConfigRequests.length;
|
||||
|
||||
const beforeConfigs = database.volumeConfigs.length;
|
||||
database.volumeConfigs = database.volumeConfigs.filter((item) => item.deviceId !== deviceId);
|
||||
const deletedConfigs = beforeConfigs - database.volumeConfigs.length;
|
||||
|
||||
const beforeNotifications = database.panelNotifications.length;
|
||||
database.panelNotifications = database.panelNotifications.filter((item) => item.deviceId !== deviceId);
|
||||
const deletedNotifications = beforeNotifications - database.panelNotifications.length;
|
||||
|
||||
const deletedLicenses = database.licenses.filter((item) =>
|
||||
item.companyId === companyId && item.productionLineId === productionLineId
|
||||
).length;
|
||||
database.licenses = database.licenses.filter((item) =>
|
||||
!(item.companyId === companyId && item.productionLineId === productionLineId)
|
||||
);
|
||||
|
||||
const deletedDirectories = await removeDeviceDirectories(deviceId);
|
||||
database.productionLines = database.productionLines.filter((item) => item.id !== productionLineId);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
deletedProductionLineId: productionLineId,
|
||||
deletedFiles: deletedFileCount,
|
||||
deletedDirectories,
|
||||
deletedLicenses,
|
||||
deletedFeedback,
|
||||
deletedVolumeRequests: deletedRequests,
|
||||
deletedVolumeConfigs: deletedConfigs,
|
||||
deletedNotifications
|
||||
};
|
||||
}
|
||||
|
||||
function backfillIdentificationFiles(database) {
|
||||
for (const record of database.fileRecords) {
|
||||
if (!record.folder.endsWith("/ind_data") || ![".csv", ".json"].includes(path.extname(record.fileName).toLowerCase())) continue;
|
||||
@@ -520,71 +588,11 @@ function createApp({
|
||||
return store.update(async (database) => {
|
||||
const company = database.companies.find((item) => item.id === companyId);
|
||||
if (!company) return { success: false, errMsg: "公司不存在" };
|
||||
const line = database.productionLines.find((item) => item.id === productionLineId && item.companyId === companyId);
|
||||
if (!line) return { success: false, errMsg: "产线不存在" };
|
||||
|
||||
const activeLicenses = database.licenses.filter((item) =>
|
||||
item.companyId === companyId && item.productionLineId === productionLineId && item.status === "active"
|
||||
);
|
||||
if (activeLicenses.length) {
|
||||
return {
|
||||
success: false,
|
||||
errMsg: `请先撤销该产线的 ${activeLicenses.length} 个有效许可证后再删除产线`,
|
||||
errCode: "ACTIVE_LICENSES_PRESENT"
|
||||
};
|
||||
}
|
||||
|
||||
const deviceId = line.deviceId;
|
||||
const relatedFileIDs = database.fileRecords
|
||||
.filter((record) => {
|
||||
if (record.fileID.startsWith(`model://${deviceId}/`)) return true;
|
||||
const prefix = `${deviceId}/`;
|
||||
return record.folder === deviceId || record.folder.startsWith(prefix);
|
||||
})
|
||||
.map((record) => record.fileID);
|
||||
|
||||
let deletedFileCount = 0;
|
||||
for (const fileID of new Set(relatedFileIDs)) {
|
||||
deletedFileCount += await removeFile(database, fileID);
|
||||
}
|
||||
|
||||
const beforeFeedback = database.identificationFeedback.length;
|
||||
database.identificationFeedback = database.identificationFeedback.filter((item) => item.deviceId !== deviceId);
|
||||
const deletedFeedback = beforeFeedback - database.identificationFeedback.length;
|
||||
|
||||
const beforeRequests = database.volumeConfigRequests.length;
|
||||
database.volumeConfigRequests = database.volumeConfigRequests.filter((item) => item.deviceId !== deviceId);
|
||||
const deletedRequests = beforeRequests - database.volumeConfigRequests.length;
|
||||
|
||||
const beforeConfigs = database.volumeConfigs.length;
|
||||
database.volumeConfigs = database.volumeConfigs.filter((item) => item.deviceId !== deviceId);
|
||||
const deletedConfigs = beforeConfigs - database.volumeConfigs.length;
|
||||
|
||||
const beforeNotifications = database.panelNotifications.length;
|
||||
database.panelNotifications = database.panelNotifications.filter((item) => item.deviceId !== deviceId);
|
||||
const deletedNotifications = beforeNotifications - database.panelNotifications.length;
|
||||
|
||||
const revokedLicenses = database.licenses.filter((item) =>
|
||||
item.companyId === companyId && item.productionLineId === productionLineId
|
||||
).length;
|
||||
database.licenses = database.licenses.filter((item) =>
|
||||
!(item.companyId === companyId && item.productionLineId === productionLineId)
|
||||
);
|
||||
|
||||
const deletedDirectories = await removeDeviceDirectories(deviceId);
|
||||
database.productionLines = database.productionLines.filter((item) => item.id !== productionLineId);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
deletedProductionLineId: productionLineId,
|
||||
deletedFiles: deletedFileCount,
|
||||
deletedDirectories,
|
||||
deletedLicenses: revokedLicenses,
|
||||
deletedFeedback,
|
||||
deletedVolumeRequests: deletedRequests,
|
||||
deletedVolumeConfigs: deletedConfigs,
|
||||
deletedNotifications
|
||||
};
|
||||
return removeProductionLineData(database, {
|
||||
companyId,
|
||||
productionLineId,
|
||||
failOnActiveLicense: true
|
||||
});
|
||||
});
|
||||
}
|
||||
case "deleteCompany": {
|
||||
@@ -592,27 +600,59 @@ function createApp({
|
||||
if (authError) return { success: false, errMsg: authError };
|
||||
const companyId = String(event.companyId || "").trim();
|
||||
if (!companyId) return { success: false, errMsg: "缺少 companyId" };
|
||||
return store.update((database) => {
|
||||
return store.update(async (database) => {
|
||||
const company = database.companies.find((item) => item.id === companyId);
|
||||
if (!company) return { success: false, errMsg: "公司不存在" };
|
||||
|
||||
const activeLicenses = database.licenses.filter((item) =>
|
||||
item.companyId === companyId && item.status === "active"
|
||||
);
|
||||
if (activeLicenses.length) {
|
||||
return {
|
||||
success: false,
|
||||
errMsg: `请先撤销该公司的 ${activeLicenses.length} 个有效许可证后再删除公司`,
|
||||
errCode: "ACTIVE_LICENSES_PRESENT"
|
||||
};
|
||||
}
|
||||
|
||||
const lines = database.productionLines.filter((item) => item.companyId === companyId);
|
||||
if (lines.length) {
|
||||
return {
|
||||
success: false,
|
||||
errMsg: `请先删除该公司的 ${lines.length} 条产线后再删除公司`,
|
||||
errCode: "PRODUCTION_LINES_PRESENT"
|
||||
};
|
||||
|
||||
const summary = {
|
||||
deletedProductionLines: 0,
|
||||
deletedFiles: 0,
|
||||
deletedDirectories: 0,
|
||||
deletedLicenses: 0,
|
||||
deletedFeedback: 0,
|
||||
deletedVolumeRequests: 0,
|
||||
deletedVolumeConfigs: 0,
|
||||
deletedNotifications: 0
|
||||
};
|
||||
|
||||
for (const line of lines) {
|
||||
const lineDeleted = await removeProductionLineData(database, {
|
||||
companyId,
|
||||
productionLineId: line.id,
|
||||
failOnActiveLicense: false
|
||||
});
|
||||
if (!lineDeleted.success) return lineDeleted;
|
||||
summary.deletedProductionLines += 1;
|
||||
summary.deletedFiles += lineDeleted.deletedFiles;
|
||||
summary.deletedDirectories += lineDeleted.deletedDirectories;
|
||||
summary.deletedLicenses += lineDeleted.deletedLicenses;
|
||||
summary.deletedFeedback += lineDeleted.deletedFeedback;
|
||||
summary.deletedVolumeRequests += lineDeleted.deletedVolumeRequests;
|
||||
summary.deletedVolumeConfigs += lineDeleted.deletedVolumeConfigs;
|
||||
summary.deletedNotifications += lineDeleted.deletedNotifications;
|
||||
}
|
||||
const licenses = database.licenses.filter((item) => item.companyId === companyId);
|
||||
if (licenses.length) {
|
||||
return {
|
||||
success: false,
|
||||
errMsg: `请先删除该公司的 ${licenses.length} 个许可证后再删除公司`,
|
||||
errCode: "LICENSES_PRESENT"
|
||||
};
|
||||
|
||||
const orphanCompanyLicenses = database.licenses.filter((item) => item.companyId === companyId).length;
|
||||
if (orphanCompanyLicenses > 0) {
|
||||
summary.deletedLicenses += orphanCompanyLicenses;
|
||||
database.licenses = database.licenses.filter((item) => item.companyId !== companyId);
|
||||
}
|
||||
|
||||
database.companies = database.companies.filter((item) => item.id !== companyId);
|
||||
return { success: true, deletedCompanyId: companyId };
|
||||
return { success: true, deletedCompanyId: companyId, ...summary };
|
||||
});
|
||||
}
|
||||
case "createLicense": {
|
||||
@@ -896,11 +936,13 @@ function createApp({
|
||||
const exists = database.panelNotifications.some((item) =>
|
||||
item.deviceId === deviceId && item.notificationId === notificationId
|
||||
);
|
||||
if (!exists) return { success: false, errMsg: "Panel 通知不存在" };
|
||||
if (!exists) {
|
||||
return { success: true, notificationId, deleted: 0, idempotent: true };
|
||||
}
|
||||
database.panelNotifications = database.panelNotifications.filter((item) =>
|
||||
!(item.deviceId === deviceId && item.notificationId === notificationId)
|
||||
);
|
||||
return { success: true, notificationId };
|
||||
return { success: true, notificationId, deleted: 1 };
|
||||
});
|
||||
}
|
||||
case "ackPanelFile": {
|
||||
|
||||
@@ -546,6 +546,13 @@ test("panel notifications and volume configuration stay isolated by device", asy
|
||||
type: "ackPanelNotification", deviceId, notificationId: notification.notification.notificationId,
|
||||
adminToken: "test-token"
|
||||
})).success, true);
|
||||
const duplicatedAck = await post({
|
||||
type: "ackPanelNotification", deviceId, notificationId: notification.notification.notificationId,
|
||||
adminToken: "test-token"
|
||||
});
|
||||
assert.equal(duplicatedAck.success, true);
|
||||
assert.equal(duplicatedAck.idempotent, true);
|
||||
assert.equal(duplicatedAck.deleted, 0);
|
||||
|
||||
async function uploadResult(folderName, fileName, content) {
|
||||
const result = await post({ type: "uploadDataFile", fileName, folder: `${deviceId}/${folderName}` });
|
||||
@@ -908,7 +915,7 @@ test("deleteProductionLine blocks active licenses and removes revoked data", asy
|
||||
assert.deepEqual(models.fileList, []);
|
||||
});
|
||||
|
||||
test("deleteCompany requires no child lines and no remaining licenses", async () => {
|
||||
test("deleteCompany cascades revoked data cleanup", async () => {
|
||||
const suffix = crypto.randomUUID().slice(0, 8);
|
||||
const company = await post({
|
||||
type: "createCompany", name: `删除公司-${suffix}`, code: `del-co-${suffix}`,
|
||||
@@ -919,21 +926,87 @@ test("deleteCompany requires no child lines and no remaining licenses", async ()
|
||||
name: "子产线", code: "line-1", adminToken: "test-token"
|
||||
});
|
||||
|
||||
const blocked = await post({ type: "deleteCompany", companyId: company.company.id, adminToken: "test-token" });
|
||||
assert.equal(blocked.success, false);
|
||||
assert.equal(blocked.errCode, "PRODUCTION_LINES_PRESENT");
|
||||
const modelIssued = await post({
|
||||
type: "issueModelUpload", deviceId: line.productionLine.deviceId,
|
||||
fileName: "company-cascade.bin", adminToken: "test-token"
|
||||
});
|
||||
const modelForm = new FormData();
|
||||
modelForm.append("file", new Blob(["cascade-model"]), "company-cascade.bin");
|
||||
assert.equal((await fetch(modelIssued.uploadMetadata.url, { method: "POST", body: modelForm })).status, 204);
|
||||
|
||||
const lineDeleted = await post({
|
||||
type: "deleteProductionLine",
|
||||
const licenseId = crypto.randomUUID();
|
||||
const payload = {
|
||||
license_id: licenseId,
|
||||
company_id: company.company.id,
|
||||
production_line_id: line.productionLine.id,
|
||||
customer: company.company.name,
|
||||
device_id: line.productionLine.deviceId,
|
||||
issued: "2026-08-01 10:00",
|
||||
expiry: "2028-08-01 10:00",
|
||||
features: "*"
|
||||
};
|
||||
await post({
|
||||
type: "createLicense", licenseId,
|
||||
companyId: company.company.id,
|
||||
productionLineId: line.productionLine.id,
|
||||
customer: payload.customer,
|
||||
issued: payload.issued,
|
||||
expiry: payload.expiry,
|
||||
features: payload.features,
|
||||
license: signLicense(payload),
|
||||
adminToken: "test-token"
|
||||
});
|
||||
assert.equal(lineDeleted.success, true);
|
||||
await post({ type: "revokeLicense", licenseId, reason: "公司删除", adminToken: "test-token" });
|
||||
|
||||
const deleted = await post({ type: "deleteCompany", companyId: company.company.id, adminToken: "test-token" });
|
||||
assert.deepEqual(deleted, { success: true, deletedCompanyId: company.company.id });
|
||||
assert.equal(deleted.success, true);
|
||||
assert.equal(deleted.deletedCompanyId, company.company.id);
|
||||
assert.ok(deleted.deletedProductionLines >= 1);
|
||||
assert.ok(deleted.deletedLicenses >= 1);
|
||||
assert.ok(deleted.deletedFiles >= 1);
|
||||
|
||||
const organizations = await post({ type: "listOrganizations", adminToken: "test-token" });
|
||||
assert.equal(organizations.companies.some((item) => item.id === company.company.id), false);
|
||||
|
||||
const models = await post({ type: "listModels", folder: `${line.productionLine.deviceId}/model_config` });
|
||||
assert.deepEqual(models.fileList, []);
|
||||
});
|
||||
|
||||
test("deleteCompany blocks when active licenses exist", async () => {
|
||||
const suffix = crypto.randomUUID().slice(0, 8);
|
||||
const company = await post({
|
||||
type: "createCompany", name: `删除公司阻断-${suffix}`, code: `del-co-block-${suffix}`,
|
||||
adminToken: "test-token"
|
||||
});
|
||||
const line = await post({
|
||||
type: "createProductionLine", companyId: company.company.id,
|
||||
name: "阻断产线", code: "line-1", adminToken: "test-token"
|
||||
});
|
||||
|
||||
const licenseId = crypto.randomUUID();
|
||||
const payload = {
|
||||
license_id: licenseId,
|
||||
company_id: company.company.id,
|
||||
production_line_id: line.productionLine.id,
|
||||
customer: company.company.name,
|
||||
device_id: line.productionLine.deviceId,
|
||||
issued: "2026-08-01 10:00",
|
||||
expiry: "2028-08-01 10:00",
|
||||
features: "*"
|
||||
};
|
||||
await post({
|
||||
type: "createLicense", licenseId,
|
||||
companyId: company.company.id,
|
||||
productionLineId: line.productionLine.id,
|
||||
customer: payload.customer,
|
||||
issued: payload.issued,
|
||||
expiry: payload.expiry,
|
||||
features: payload.features,
|
||||
license: signLicense(payload),
|
||||
adminToken: "test-token"
|
||||
});
|
||||
|
||||
const blocked = await post({ type: "deleteCompany", companyId: company.company.id, adminToken: "test-token" });
|
||||
assert.equal(blocked.success, false);
|
||||
assert.equal(blocked.errCode, "ACTIVE_LICENSES_PRESENT");
|
||||
});
|
||||
Reference in New Issue
Block a user