Skip to content

Commit

Permalink
fix: conflicted trigger should return error on POST
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucky3028 committed Oct 4, 2023
1 parent a6fd2bb commit 1338dcc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 11 additions & 2 deletions apps/auto-run-ac-api/src/api/defaultTriggersApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export const defaultTriggersApi = app
},
},
},
409: {
description: 'The trigger has already been created',
},
},
method: 'post',
path: '/',
Expand All @@ -63,7 +66,7 @@ export const defaultTriggersApi = app
const contents = c.req.valid('json');

const id = nanoid();
await c
const createdTriggers = await c
.get('db')
.insert(table)
.values({
Expand All @@ -73,7 +76,13 @@ export const defaultTriggersApi = app
settingsTemp: contents.ac.temp,
operationMode: contents.ac.mode,
})
.onConflictDoNothing();
.onConflictDoNothing()
.returning({ createdId: table.id });

if (createdTriggers.length === 0) {
return emptyJsonT(c, 409);
}

const trigger = { ...contents, id };

return c.jsonT({ trigger }, 201, { Location: `${getUrl(c.req)}/${id}` });
Expand Down
13 changes: 11 additions & 2 deletions apps/auto-run-ac-api/src/api/triggersApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export const triggersApi = app
},
},
},
409: {
description: 'The trigger has already been created',
},
},
method: 'post',
path: '/',
Expand All @@ -63,7 +66,7 @@ export const triggersApi = app
const contents = c.req.valid('json');

const id = nanoid();
await c
const createdTrigger = await c
.get('db')
.insert(table)
.values({
Expand All @@ -73,7 +76,13 @@ export const triggersApi = app
settingsTemp: contents.ac.temp,
operationMode: contents.ac.mode,
})
.onConflictDoNothing();
.onConflictDoNothing()
.returning({ createdId: table.id });

if (createdTrigger.length === 0) {
return emptyJsonT(c, 409);
}

const trigger = { ...contents, id };

return c.jsonT({ trigger }, 201, { Location: `${getUrl(c.req)}/${id}` });
Expand Down

0 comments on commit 1338dcc

Please sign in to comment.