fix del api
This commit is contained in:
@@ -371,6 +371,28 @@ function createApp({
|
||||
return before - database.fileRecords.length;
|
||||
}
|
||||
|
||||
async function removeDeviceDirectories(deviceId) {
|
||||
const segments = String(deviceId || "").split("/");
|
||||
if (segments.length !== 2 || !segments[0] || !segments[1]) return 0;
|
||||
const [companyCode, lineCode] = segments;
|
||||
const targets = [
|
||||
path.join(store.modelsDirectory, companyCode, lineCode),
|
||||
path.join(store.filesDirectory, "ReinLoop_GUI", companyCode, lineCode)
|
||||
];
|
||||
let removed = 0;
|
||||
for (const directory of targets) {
|
||||
try {
|
||||
const stat = await fs.promises.stat(directory);
|
||||
if (!stat.isDirectory()) continue;
|
||||
} catch (_error) {
|
||||
continue;
|
||||
}
|
||||
await fs.promises.rm(directory, { recursive: true, force: true });
|
||||
removed += 1;
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
function backfillIdentificationFiles(database) {
|
||||
for (const record of database.fileRecords) {
|
||||
if (!record.folder.endsWith("/ind_data") || ![".csv", ".json"].includes(path.extname(record.fileName).toLowerCase())) continue;
|
||||
@@ -549,12 +571,14 @@ function createApp({
|
||||
!(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,
|
||||
|
||||
@@ -877,6 +877,14 @@ test("deleteProductionLine blocks active licenses and removes revoked data", asy
|
||||
assert.equal(blocked.errCode, "ACTIVE_LICENSES_PRESENT");
|
||||
|
||||
await post({ type: "revokeLicense", licenseId, reason: "产线删除", adminToken: "test-token" });
|
||||
|
||||
const orphanModelDir = path.join(dataDirectory, "models", line.productionLine.deviceId);
|
||||
const orphanFileDir = path.join(dataDirectory, "files", "ReinLoop_GUI", line.productionLine.deviceId);
|
||||
await fs.promises.mkdir(orphanModelDir, { recursive: true });
|
||||
await fs.promises.mkdir(orphanFileDir, { recursive: true });
|
||||
await fs.promises.writeFile(path.join(orphanModelDir, "orphan.bin"), "orphan-model");
|
||||
await fs.promises.writeFile(path.join(orphanFileDir, "orphan.json"), "orphan-file");
|
||||
|
||||
const deleted = await post({
|
||||
type: "deleteProductionLine",
|
||||
companyId: company.company.id,
|
||||
@@ -886,6 +894,10 @@ test("deleteProductionLine blocks active licenses and removes revoked data", asy
|
||||
assert.equal(deleted.success, true);
|
||||
assert.ok(deleted.deletedFiles >= 1);
|
||||
assert.ok(deleted.deletedLicenses >= 1);
|
||||
assert.ok(deleted.deletedDirectories >= 1);
|
||||
|
||||
await assert.rejects(fs.promises.access(orphanModelDir));
|
||||
await assert.rejects(fs.promises.access(orphanFileDir));
|
||||
|
||||
const organizations = await post({ type: "listOrganizations", adminToken: "test-token" });
|
||||
const listedCompany = organizations.companies.find((item) => item.id === company.company.id);
|
||||
|
||||
Reference in New Issue
Block a user