-
Notifications
You must be signed in to change notification settings - Fork 336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow 2 custom templates for every user #9518
Changes from 3 commits
49db174
f296e5f
6bbb592
a5942ee
5ac330e
32a0211
24c87e0
b85fb31
71f4d9a
dbec412
7e3befd
f71105a
ec37ff9
2c99d96
cb0f353
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,27 @@ | ||
import getPg from '../getPg' | ||
import {decrementFreeRetroTemplatesRemainingQuery} from './generated/decrementFreeCustomRetroTemplatesRemainingQuery' | ||
import {decrementFreePokerTemplatesRemainingQuery} from './generated/decrementFreeCustomPokerTemplatesRemainingQuery' | ||
import getKysely from '../getKysely' | ||
|
||
const decrementFreeTemplatesRemaining = async (userId: string, templateType: 'retro' | 'poker') => { | ||
const res = | ||
const pg = getKysely() | ||
const customTemplateType = | ||
templateType === 'retro' | ||
? await decrementFreeRetroTemplatesRemainingQuery.run({userId}, getPg()) | ||
: await decrementFreePokerTemplatesRemainingQuery.run({userId}, getPg()) | ||
return res | ||
? 'freeCustomRetroTemplatesRemaining' | ||
: 'freeCustomPokerTemplatesRemaining' | ||
|
||
const userBeforeUpdate = await pg | ||
.selectFrom('User') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -1 this is slightly worse than the SQL you had because if this function gets called twice then we risk the first query being a stale read. If there's ever a way to do something in a single SQL command, that should be the goal! See Playground Link const person = await db
.updateTable('User')
.set((eb) => ({[field]: eb(field, '-', '1')} ))
.where('id', '=', userId)
.where(field, '>', 0)
.executeTakeFirst() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, thanks! Updated |
||
.select(customTemplateType) | ||
.where('id', '=', userId) | ||
.executeTakeFirst() | ||
|
||
if (userBeforeUpdate && userBeforeUpdate[customTemplateType] > 0) { | ||
await pg | ||
.updateTable('User') | ||
.set({ | ||
[customTemplateType]: userBeforeUpdate[customTemplateType] - 1 | ||
}) | ||
.where('id', '=', userId) | ||
.execute() | ||
} | ||
} | ||
|
||
export default decrementFreeTemplatesRemaining |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-1 pg-typed is deprecated. can we use kysely instead here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nickoferrall thoughts on changing this to kysely?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I've updated the migration to kysely, but not the query. I can do this too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated!