fix license check

This commit is contained in:
2026-08-03 15:18:28 +08:00
parent 2090597858
commit bae71f3253
11 changed files with 469 additions and 20 deletions
+123
View File
@@ -487,6 +487,110 @@ function createApp({
return { success: true, productionLine };
});
}
case "deleteProductionLine": {
const authError = requireAdmin(event);
if (authError) return { success: false, errMsg: authError };
const companyId = String(event.companyId || "").trim();
const productionLineId = String(event.productionLineId || "").trim();
if (!companyId || !productionLineId) {
return { success: false, errMsg: "缺少 companyId 或 productionLineId" };
}
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)
);
database.productionLines = database.productionLines.filter((item) => item.id !== productionLineId);
return {
success: true,
deletedProductionLineId: productionLineId,
deletedFiles: deletedFileCount,
deletedLicenses: revokedLicenses,
deletedFeedback,
deletedVolumeRequests: deletedRequests,
deletedVolumeConfigs: deletedConfigs,
deletedNotifications
};
});
}
case "deleteCompany": {
const authError = requireAdmin(event);
if (authError) return { success: false, errMsg: authError };
const companyId = String(event.companyId || "").trim();
if (!companyId) return { success: false, errMsg: "缺少 companyId" };
return store.update((database) => {
const company = database.companies.find((item) => item.id === companyId);
if (!company) return { success: false, errMsg: "公司不存在" };
const lines = database.productionLines.filter((item) => item.companyId === companyId);
if (lines.length) {
return {
success: false,
errMsg: `请先删除该公司的 ${lines.length} 条产线后再删除公司`,
errCode: "PRODUCTION_LINES_PRESENT"
};
}
const licenses = database.licenses.filter((item) => item.companyId === companyId);
if (licenses.length) {
return {
success: false,
errMsg: `请先删除该公司的 ${licenses.length} 个许可证后再删除公司`,
errCode: "LICENSES_PRESENT"
};
}
database.companies = database.companies.filter((item) => item.id !== companyId);
return { success: true, deletedCompanyId: companyId };
});
}
case "createLicense": {
const authError = requireAdmin(event);
if (authError) return { success: false, errMsg: authError };
@@ -585,6 +689,25 @@ function createApp({
logRevokeAction({ event, licenseId, success: result.success, errCode: result.errCode });
return result;
}
case "deleteLicense": {
const authError = requireAdmin(event);
if (authError) return { success: false, errMsg: authError };
const licenseId = String(event.licenseId || "").trim();
if (!licenseId) return { success: false, errMsg: "licenseId 不能为空", errCode: "LICENSE_ID_REQUIRED" };
return store.update((database) => {
const record = database.licenses.find((item) => item.licenseId === licenseId);
if (!record) return { success: false, errMsg: "许可证不存在", errCode: "LICENSE_NOT_FOUND" };
if (record.status === "active") {
return {
success: false,
errMsg: "请先撤销许可证后再删除",
errCode: "LICENSE_ACTIVE"
};
}
database.licenses = database.licenses.filter((item) => item.licenseId !== licenseId);
return { success: true, deletedLicenseId: licenseId };
});
}
case "validateLicense": {
const database = await store.read();
const record = database.licenses.find((item) => item.licenseId === event.licenseId);