Skip to content
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

Merged
merged 15 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/server/graphql/public/types/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ const User: UserResolvers = {
},
tier: ({tier, trialStartDate}) => {
return getFeatureTier({tier, trialStartDate})
}
},
billingTier: ({tier}) => tier
}

export default User
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 =
Copy link
Member

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?

Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

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')
Copy link
Member

Choose a reason for hiding this comment

The 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() 

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.