mirror of
https://github.com/fosrl/pangolin.git
synced 2026-01-29 06:10:47 +00:00
112 lines
3.4 KiB
TypeScript
112 lines
3.4 KiB
TypeScript
import { Request, Response, NextFunction } from "express";
|
|
import { z } from "zod";
|
|
import { db, olms } from "@server/db";
|
|
import { clients, clientSitesAssociationsCache } from "@server/db";
|
|
import { eq } from "drizzle-orm";
|
|
import response from "@server/lib/response";
|
|
import HttpCode from "@server/types/HttpCode";
|
|
import createHttpError from "http-errors";
|
|
import logger from "@server/logger";
|
|
import { fromError } from "zod-validation-error";
|
|
import { OpenAPITags, registry } from "@server/openApi";
|
|
import { rebuildClientAssociationsFromClient } from "@server/lib/rebuildClientAssociations";
|
|
import { sendTerminateClient } from "./terminate";
|
|
|
|
const deleteClientSchema = z.strictObject({
|
|
clientId: z.string().transform(Number).pipe(z.int().positive())
|
|
});
|
|
|
|
registry.registerPath({
|
|
method: "delete",
|
|
path: "/client/{clientId}",
|
|
description: "Delete a client by its client ID.",
|
|
tags: [OpenAPITags.Client],
|
|
request: {
|
|
params: deleteClientSchema
|
|
},
|
|
responses: {}
|
|
});
|
|
|
|
export async function deleteClient(
|
|
req: Request,
|
|
res: Response,
|
|
next: NextFunction
|
|
): Promise<any> {
|
|
try {
|
|
const parsedParams = deleteClientSchema.safeParse(req.params);
|
|
if (!parsedParams.success) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.BAD_REQUEST,
|
|
fromError(parsedParams.error).toString()
|
|
)
|
|
);
|
|
}
|
|
|
|
const { clientId } = parsedParams.data;
|
|
|
|
const [client] = await db
|
|
.select()
|
|
.from(clients)
|
|
.where(eq(clients.clientId, clientId))
|
|
.limit(1);
|
|
|
|
if (!client) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.NOT_FOUND,
|
|
`Client with ID ${clientId} not found`
|
|
)
|
|
);
|
|
}
|
|
|
|
// Only allow deletion of machine clients (clients without userId)
|
|
if (client.userId) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.BAD_REQUEST,
|
|
`Cannot delete a user client. User clients must be archived instead.`
|
|
)
|
|
);
|
|
}
|
|
|
|
await db.transaction(async (trx) => {
|
|
// Then delete the client itself
|
|
const [deletedClient] = await trx
|
|
.delete(clients)
|
|
.where(eq(clients.clientId, clientId))
|
|
.returning();
|
|
|
|
const [olm] = await trx
|
|
.select()
|
|
.from(olms)
|
|
.where(eq(olms.clientId, clientId))
|
|
.limit(1);
|
|
|
|
// this is a machine client so we also delete the olm
|
|
if (!client.userId && client.olmId) {
|
|
await trx.delete(olms).where(eq(olms.olmId, client.olmId));
|
|
}
|
|
|
|
await rebuildClientAssociationsFromClient(deletedClient, trx);
|
|
|
|
if (olm) {
|
|
await sendTerminateClient(deletedClient.clientId, olm.olmId); // the olmId needs to be provided because it cant look it up after deletion
|
|
}
|
|
});
|
|
|
|
return response(res, {
|
|
data: null,
|
|
success: true,
|
|
error: false,
|
|
message: "Client deleted successfully",
|
|
status: HttpCode.OK
|
|
});
|
|
} catch (error) {
|
|
logger.error(error);
|
|
return next(
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
|
);
|
|
}
|
|
}
|