Skip to content

Commit

Permalink
Fix for available invite creation (#767)
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy authored Apr 6, 2023
1 parent 831b7da commit e8e5a88
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export default function (server: Server, ctx: AppContext) {
const couldCreate = Math.floor(
accountLifespan / ctx.cfg.userInviteInterval,
)
const toCreate = Math.min(5 - unusedCodes.length, couldCreate)
const toCreate = Math.min(
5 - unusedCodes.length,
couldCreate - userCodes.length,
)
if (toCreate > 0) {
created = genInvCodes(ctx.cfg, toCreate)
const rows = created.map((code) => ({
Expand Down
30 changes: 17 additions & 13 deletions packages/pds/tests/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,7 @@ describe('account', () => {
const res1 = await agent.api.com.atproto.server.getAccountInviteCodes()
expect(res1.data.codes.length).toBe(2)

// now pretend it was made 10 days ago & use both invites
const tenDaysAgo = new Date(Date.now() - 10 * DAY).toISOString()
await ctx.db.db
.updateTable('user_account')
.set({ createdAt: tenDaysAgo })
.where('did', '=', did)
.execute()
// use both invites and confirm we can't get any more
await ctx.db.db
.insertInto('invite_code_use')
.values(
Expand All @@ -492,18 +486,28 @@ describe('account', () => {
})),
)
.execute()
const res2 = await agent.api.com.atproto.server.getAccountInviteCodes()
expect(res2.data.codes.length).toBe(2)

// now pretend it was made 10 days ago
const tenDaysAgo = new Date(Date.now() - 10 * DAY).toISOString()
await ctx.db.db
.updateTable('user_account')
.set({ createdAt: tenDaysAgo })
.where('did', '=', did)
.execute()

const res2 = await agent.api.com.atproto.server.getAccountInviteCodes({
const res3 = await agent.api.com.atproto.server.getAccountInviteCodes({
includeUsed: false,
createAvailable: false,
})
expect(res2.data.codes.length).toBe(0)
const res3 = await agent.api.com.atproto.server.getAccountInviteCodes()
expect(res3.data.codes.length).toBe(7)
const res4 = await agent.api.com.atproto.server.getAccountInviteCodes({
expect(res3.data.codes.length).toBe(0)
const res4 = await agent.api.com.atproto.server.getAccountInviteCodes()
expect(res4.data.codes.length).toBe(7)
const res5 = await agent.api.com.atproto.server.getAccountInviteCodes({
includeUsed: false,
})
expect(res4.data.codes.length).toBe(5)
expect(res5.data.codes.length).toBe(5)
})

it('prevents use of disabled codes', async () => {
Expand Down

0 comments on commit e8e5a88

Please sign in to comment.