-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(deployment): implement deployment settings api
refs #714
- Loading branch information
1 parent
600c7cd
commit 9072449
Showing
10 changed files
with
564 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
apps/api/src/deployment/controllers/deployment-setting/deployment-setting.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import assert from "http-assert"; | ||
import { singleton } from "tsyringe"; | ||
|
||
import { Protected } from "@src/auth/services/auth.service"; | ||
import { | ||
CreateDeploymentSettingRequest, | ||
DeploymentSettingResponse, | ||
FindDeploymentSettingParams, | ||
UpdateDeploymentSettingRequest | ||
} from "@src/deployment/http-schemas/deployment-setting.schema"; | ||
import { DeploymentSettingService } from "@src/deployment/services/deployment-setting/deployment-setting.service"; | ||
|
||
@singleton() | ||
export class DeploymentSettingController { | ||
constructor(private readonly deploymentSettingService: DeploymentSettingService) {} | ||
|
||
@Protected([{ action: "read", subject: "DeploymentSetting" }]) | ||
async findByUserIdAndDseq(params: FindDeploymentSettingParams): Promise<DeploymentSettingResponse> { | ||
const setting = await this.deploymentSettingService.findByUserIdAndDseq(params); | ||
assert(setting, 404, "Deployment setting not found"); | ||
return { data: setting }; | ||
} | ||
|
||
@Protected([{ action: "create", subject: "DeploymentSetting" }]) | ||
async create(input: CreateDeploymentSettingRequest["data"]): Promise<DeploymentSettingResponse> { | ||
const setting = await this.deploymentSettingService.create(input); | ||
return { data: setting }; | ||
} | ||
|
||
@Protected([{ action: "update", subject: "DeploymentSetting" }]) | ||
async update(params: FindDeploymentSettingParams, input: UpdateDeploymentSettingRequest["data"]): Promise<DeploymentSettingResponse> { | ||
const setting = await this.deploymentSettingService.update(params, input); | ||
assert(setting, 404, "Deployment setting not found"); | ||
return { data: setting }; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
apps/api/src/deployment/http-schemas/deployment-setting.schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { z } from "zod"; | ||
|
||
const DeploymentSettingSchema = z.object({ | ||
id: z.number(), | ||
userId: z.string(), | ||
dseq: z.string(), | ||
autoTopUpEnabled: z.boolean(), | ||
createdAt: z.string().datetime(), | ||
updatedAt: z.string().datetime() | ||
}); | ||
|
||
export const DeploymentSettingResponseSchema = z.object({ | ||
data: DeploymentSettingSchema | ||
}); | ||
|
||
export const CreateDeploymentSettingRequestSchema = z.object({ | ||
data: z.object({ | ||
userId: z.string().openapi({ | ||
description: "User ID" | ||
}), | ||
dseq: z.string().openapi({ | ||
description: "Deployment sequence number" | ||
}), | ||
autoTopUpEnabled: z.boolean().default(false).openapi({ | ||
description: "Whether auto top-up is enabled for this deployment" | ||
}) | ||
}) | ||
}); | ||
|
||
export const UpdateDeploymentSettingRequestSchema = z.object({ | ||
data: z.object({ | ||
autoTopUpEnabled: z.boolean().openapi({ | ||
description: "Whether auto top-up is enabled for this deployment" | ||
}) | ||
}) | ||
}); | ||
|
||
export const FindDeploymentSettingParamsSchema = z.object({ | ||
userId: z.string().openapi({ | ||
description: "User ID" | ||
}), | ||
dseq: z.string().openapi({ | ||
description: "Deployment sequence number" | ||
}) | ||
}); | ||
|
||
export type DeploymentSetting = z.infer<typeof DeploymentSettingSchema>; | ||
export type DeploymentSettingResponse = z.infer<typeof DeploymentSettingResponseSchema>; | ||
export type CreateDeploymentSettingRequest = z.infer<typeof CreateDeploymentSettingRequestSchema>; | ||
export type UpdateDeploymentSettingRequest = z.infer<typeof UpdateDeploymentSettingRequestSchema>; | ||
export type FindDeploymentSettingParams = z.infer<typeof FindDeploymentSettingParamsSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
apps/api/src/deployment/routes/deployment-setting/deployment-setting.router.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { createRoute as createOpenApiRoute } from "@hono/zod-openapi"; | ||
import { container } from "tsyringe"; | ||
import { z } from "zod"; | ||
|
||
import { OpenApiHonoHandler } from "@src/core/services/open-api-hono-handler/open-api-hono-handler"; | ||
import { DeploymentSettingController } from "@src/deployment/controllers/deployment-setting/deployment-setting.controller"; | ||
import { | ||
CreateDeploymentSettingRequestSchema, | ||
DeploymentSettingResponseSchema, | ||
FindDeploymentSettingParamsSchema, | ||
UpdateDeploymentSettingRequestSchema | ||
} from "@src/deployment/http-schemas/deployment-setting.schema"; | ||
|
||
const getRoute = createOpenApiRoute({ | ||
method: "get", | ||
path: "/v1/deployment-settings/{userId}/{dseq}", | ||
summary: "Get deployment settings by user ID and dseq", | ||
tags: ["Deployment Settings"], | ||
request: { | ||
params: FindDeploymentSettingParamsSchema | ||
}, | ||
responses: { | ||
200: { | ||
description: "Returns deployment settings", | ||
content: { | ||
"application/json": { | ||
schema: DeploymentSettingResponseSchema | ||
} | ||
} | ||
}, | ||
404: { | ||
description: "Deployment settings not found", | ||
content: { | ||
"application/json": { | ||
schema: z.object({ | ||
message: z.string() | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
const postRoute = createOpenApiRoute({ | ||
method: "post", | ||
path: "/v1/deployment-settings", | ||
summary: "Create deployment settings", | ||
tags: ["Deployment Settings"], | ||
request: { | ||
body: { | ||
content: { | ||
"application/json": { | ||
schema: CreateDeploymentSettingRequestSchema | ||
} | ||
} | ||
} | ||
}, | ||
responses: { | ||
201: { | ||
description: "Deployment settings created successfully", | ||
content: { | ||
"application/json": { | ||
schema: DeploymentSettingResponseSchema | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
const patchRoute = createOpenApiRoute({ | ||
method: "patch", | ||
path: "/v1/deployment-settings/{userId}/{dseq}", | ||
summary: "Update deployment settings", | ||
tags: ["Deployment Settings"], | ||
request: { | ||
params: FindDeploymentSettingParamsSchema, | ||
body: { | ||
content: { | ||
"application/json": { | ||
schema: UpdateDeploymentSettingRequestSchema | ||
} | ||
} | ||
} | ||
}, | ||
responses: { | ||
200: { | ||
description: "Deployment settings updated successfully", | ||
content: { | ||
"application/json": { | ||
schema: DeploymentSettingResponseSchema | ||
} | ||
} | ||
}, | ||
404: { | ||
description: "Deployment settings not found", | ||
content: { | ||
"application/json": { | ||
schema: z.object({ | ||
message: z.string() | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
export const deploymentSettingRouter = new OpenApiHonoHandler(); | ||
|
||
deploymentSettingRouter.openapi(getRoute, async function routeGetDeploymentSettings(c) { | ||
const params = c.req.valid("param"); | ||
const result = await container.resolve(DeploymentSettingController).findByUserIdAndDseq(params); | ||
|
||
return c.json(result, 200); | ||
}); | ||
|
||
deploymentSettingRouter.openapi(postRoute, async function routeCreateDeploymentSettings(c) { | ||
const { data } = c.req.valid("json"); | ||
const result = await container.resolve(DeploymentSettingController).create(data); | ||
return c.json(result, 201); | ||
}); | ||
|
||
deploymentSettingRouter.openapi(patchRoute, async function routeUpdateDeploymentSettings(c) { | ||
const params = c.req.valid("param"); | ||
const { data } = c.req.valid("json"); | ||
const result = await container.resolve(DeploymentSettingController).update(params, data); | ||
return c.json(result, 200); | ||
}); |
29 changes: 29 additions & 0 deletions
29
apps/api/src/deployment/services/deployment-setting/deployment-setting.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { singleton } from "tsyringe"; | ||
|
||
import { AuthService } from "@src/auth/services/auth.service"; | ||
import { FindDeploymentSettingParams } from "@src/deployment/http-schemas/deployment-setting.schema"; | ||
import { | ||
DeploymentSettingRepository, | ||
DeploymentSettingsInput, | ||
DeploymentSettingsOutput | ||
} from "@src/deployment/repositories/deployment-setting/deployment-setting.repository"; | ||
|
||
@singleton() | ||
export class DeploymentSettingService { | ||
constructor( | ||
private readonly deploymentSettingRepository: DeploymentSettingRepository, | ||
private readonly authService: AuthService | ||
) {} | ||
|
||
async findByUserIdAndDseq(params: FindDeploymentSettingParams): Promise<DeploymentSettingsOutput> { | ||
return await this.deploymentSettingRepository.accessibleBy(this.authService.ability, "read").findOneBy(params); | ||
} | ||
|
||
async create(input: DeploymentSettingsInput): Promise<DeploymentSettingsOutput> { | ||
return await this.deploymentSettingRepository.accessibleBy(this.authService.ability, "create").create(input); | ||
} | ||
|
||
async update(params: FindDeploymentSettingParams, input: Pick<DeploymentSettingsInput, "autoTopUpEnabled">): Promise<DeploymentSettingsOutput> { | ||
return await this.deploymentSettingRepository.accessibleBy(this.authService.ability, "update").updateBy(params, input, { returning: true }); | ||
} | ||
} |
Oops, something went wrong.