Skip to content

Commit

Permalink
feat(deployment): creates setting if not exists on get
Browse files Browse the repository at this point in the history
refs #714
  • Loading branch information
ygrishajev committed Feb 6, 2025
1 parent 8fc4523 commit c9ced2d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ 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);
async findOrCreateByUserIdAndDseq(params: FindDeploymentSettingParams): Promise<DeploymentSettingResponse> {
const setting = await this.deploymentSettingService.findOrCreateByUserIdAndDseq(params);
assert(setting, 404, "Deployment setting not found");
return { data: setting };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ 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);
const result = await container.resolve(DeploymentSettingController).findOrCreateByUserIdAndDseq(params);

return c.json(result, 200);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,21 @@ export class DeploymentSettingService {
private readonly config: DeploymentConfigService
) {}

async findByUserIdAndDseq(params: FindDeploymentSettingParams): Promise<DeploymentSettingWithEstimatedTopUpAmount | undefined> {
return this.withEstimatedTopUpAmount(await this.deploymentSettingRepository.accessibleBy(this.authService.ability, "read").findOneBy(params));
async findOrCreateByUserIdAndDseq(params: FindDeploymentSettingParams): Promise<DeploymentSettingWithEstimatedTopUpAmount | undefined> {
const setting = await this.deploymentSettingRepository.accessibleBy(this.authService.ability, "read").findOneBy(params);

if (setting) {
return this.withEstimatedTopUpAmount(setting);
}

try {
return await this.create(params);
} catch (error) {
if (error instanceof ForbiddenError) {
return undefined;
}
throw error;
}
}

async create(input: DeploymentSettingsInput): Promise<DeploymentSettingWithEstimatedTopUpAmount> {
Expand Down
21 changes: 14 additions & 7 deletions apps/api/test/functional/deployment-setting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,28 @@ describe("Deployment Settings", () => {
expect(response.status).toBe(401);
});

it("should return 404 if deployment settings not found", async () => {
const { token } = await walletService.createUserAndWallet();
const userId = faker.string.uuid();
it("should return a new deployment setting if not found", async () => {
const { token, user } = await walletService.createUserAndWallet();
const dseq = faker.string.numeric();

const response = await app.request(`/v1/deployment-settings/${userId}/${dseq}`, {
const response = await app.request(`/v1/deployment-settings/${user.id}/${dseq}`, {
headers: {
authorization: `Bearer ${token}`
}
});

expect(response.status).toBe(404);
expect(response.status).toBe(200);
expect(await response.json()).toEqual({
error: "NotFoundError",
message: "Deployment setting not found"
data: {
id: expect.any(String),
userId: user.id,
dseq,
autoTopUpEnabled: false,
createdAt: expect.any(String),
updatedAt: expect.any(String),
estimatedTopUpAmount: expect.any(Number),
topUpFrequencyMs: expect.any(Number)
}
});
});

Expand Down

0 comments on commit c9ced2d

Please sign in to comment.