Skip to content

Commit a674ef8

Browse files
authored
Merge pull request #179 from flowcore-io/add-user-delete-command
feat: add UserDeleteCommand to handle deletion of the authenticated user
2 parents 8114d69 + b1c1ea4 commit a674ef8

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

src/commands/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export * from "./security/permissions.list.ts"
8787

8888
// User
8989
export * from "./user/user.initialize-in-keycloak.ts"
90+
export * from "./user/user.delete.ts"
9091

9192
// Scenario
9293
export * from "./scenario/scenario.create.ts"

src/commands/user/user.delete.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Type } from "@sinclair/typebox"
2+
import { Command } from "../../common/command.ts"
3+
import { parseResponseHelper } from "../../utils/parse-response-helper.ts"
4+
import type { Static, TObject, TString } from "@sinclair/typebox"
5+
6+
/**
7+
* The input for deleting the current authenticated user
8+
* No input parameters needed - checks current authenticated user
9+
*/
10+
export type UserDeleteInput = Record<PropertyKey, never>
11+
12+
/**
13+
* The output for deleting the current authenticated user
14+
*/
15+
export type UserDeleteOutput = Static<typeof responseSchema>
16+
17+
const responseSchema: TObject<{
18+
id: TString
19+
}> = Type.Object({
20+
id: Type.String(),
21+
})
22+
23+
/**
24+
* Delete current authenticated user
25+
*/
26+
export class UserDeleteCommand extends Command<
27+
UserDeleteInput,
28+
UserDeleteOutput
29+
> {
30+
/**
31+
* The allowed modes for the command
32+
*/
33+
protected override allowedModes: ("apiKey" | "bearer")[] = ["bearer"]
34+
35+
/**
36+
* Get the base URL for the request
37+
*/
38+
protected override getBaseUrl(): string {
39+
return "https://user-2.api.flowcore.io"
40+
}
41+
42+
/**
43+
* Get the method
44+
*/
45+
protected override getMethod(): string {
46+
return "DELETE"
47+
}
48+
49+
/**
50+
* Get the path for the request
51+
*/
52+
protected override getPath(): string {
53+
return `/api/users`
54+
}
55+
56+
/**
57+
* Parse the response
58+
*/
59+
protected override parseResponse(rawResponse: unknown): UserDeleteOutput {
60+
const response = parseResponseHelper(responseSchema, rawResponse)
61+
return response
62+
}
63+
}

test/tests/commands/user.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assertEquals } from "@std/assert"
22
import { afterAll, afterEach, describe, it } from "@std/testing/bdd"
3-
import { FlowcoreClient, UserInitializeInKeycloakCommand } from "../../../src/mod.ts"
3+
import { FlowcoreClient, UserDeleteCommand, UserInitializeInKeycloakCommand } from "../../../src/mod.ts"
44
import { FetchMocker } from "../../fixtures/fetch.fixture.ts"
55

66
describe("User", () => {
@@ -68,4 +68,26 @@ describe("User", () => {
6868
assertEquals(response, userData)
6969
})
7070
})
71+
72+
describe("UserDeleteCommand", () => {
73+
it("should DELETE /api/users with an empty body and return id", async () => {
74+
// arrange
75+
const responseData = { id: "user123" }
76+
77+
fetchMockerBuilder.delete("/api/users")
78+
.matchHeaders({
79+
Authorization: "Bearer BEARER_TOKEN",
80+
"Content-Type": "application/json",
81+
})
82+
.matchBody({})
83+
.respondWith(200, responseData)
84+
85+
// act
86+
const command = new UserDeleteCommand({})
87+
const response = await flowcoreClient.execute(command)
88+
89+
// assert
90+
assertEquals(response, responseData)
91+
})
92+
})
7193
})

0 commit comments

Comments
 (0)