34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
|
|
import assert from "node:assert/strict";
|
||
|
|
|
||
|
|
import { InventoryResponseSchema } from "@metis/assets/common";
|
||
|
|
|
||
|
|
type Request = (path: string, body?: unknown) => Promise<Response>;
|
||
|
|
|
||
|
|
export async function unlinkAssetFromWorkstations(
|
||
|
|
request: Request,
|
||
|
|
companyId: string,
|
||
|
|
assetId: string
|
||
|
|
) {
|
||
|
|
const inventory = InventoryResponseSchema.parse(
|
||
|
|
await (await request(`/assets/${companyId}`)).json()
|
||
|
|
);
|
||
|
|
|
||
|
|
for (const station of inventory.workstations.filter(
|
||
|
|
(item) =>
|
||
|
|
item.values.active &&
|
||
|
|
[...item.values.assetIds, ...item.values.sharedAssetIds].includes(assetId)
|
||
|
|
)) {
|
||
|
|
const response = await request(`/assets/${companyId}/workstations/${station.id}/update`, {
|
||
|
|
values: {
|
||
|
|
...station.values,
|
||
|
|
computerAssetId:
|
||
|
|
station.values.computerAssetId === assetId ? null : station.values.computerAssetId,
|
||
|
|
assetIds: station.values.assetIds.filter((id) => id !== assetId),
|
||
|
|
sharedAssetIds: station.values.sharedAssetIds.filter((id) => id !== assetId),
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
assert.equal(response.status, 200, await response.clone().text());
|
||
|
|
}
|
||
|
|
}
|