feat: Invite codes
2 unresolved threads
2 unresolved threads
Created by: Mogge

Pullrequest

Backend part for using invite codes for user registration
Merge request reports
Activity
Filter activity
1 export default function generateInviteCode() { 2 // 6 random numbers in [ 0, 35 ] are 36 possible numbers (10 [0-9] + 26 [A-Z]) 3 return Array.from({ length: 6 }, (n = Math.floor(Math.random() * 36)) => { 4 // n > 9: it is a letter (ASCII 65 is A) -> 10 + 55 = 65 5 // else: it is a number (ASCII 48 is 0) -> 0 + 48 = 48 6 return String.fromCharCode(n > 9 ? n + 55 : n + 48) Created by: ulfgebhardt
Here a comment would be awesome ;). Took me a sec to understand. Also
55
is incorrect - should be65
. See https://en.wikipedia.org/wiki/ASCII
183 184 it('does not validate an invite code which expired in the past', async () => { 185 const lastWeek = new Date() 186 lastWeek.setDate(lastWeek.getDate() - 7) 187 const inviteCode = await Factory.build('inviteCode', { 188 expiresAt: lastWeek.toISOString(), 189 }) 190 const code = inviteCode.get('code') 191 const result = await query({ query: isValidInviteCodeQuery, variables: { code } }) 192 expect(result.data.isValidInviteCode).toBeFalsy() 193 }) 194 195 it('does not validate an invite code which does not exits', async () => { 196 const result = await query({ query: isValidInviteCodeQuery, variables: { code: 'AAA' } }) 197 expect(result.data.isValidInviteCode).toBeFalsy() 198 }) 8 }) 9 return parseInt(String(result.records[0].get('count'))) === 0 10 }) 11 } 12 13 export default { 14 Query: { 15 MyInviteCodes: async (_parent, args, context, _resolveInfo) => { 16 const { 17 user: { id: userId }, 18 } = context 19 const session = context.driver.session() 20 const readTxResultPromise = session.readTransaction(async (txc) => { 21 const result = await txc.run( 22 `MATCH (user:User {id: $userId})-[:GENERATED]->(ic:InviteCode) 23 RETURN properties(ic) AS inviteCodes`, 33 } finally { 34 session.close() 35 } 36 }, 37 isValidInviteCode: async (_parent, args, context, _resolveInfo) => { 38 const { code } = args 39 if (!code) return false 40 const session = context.driver.session() 41 const readTxResultPromise = session.readTransaction(async (txc) => { 42 const result = await txc.run( 43 `MATCH (ic:InviteCode { code: toUpper($code) }) 44 RETURN 45 CASE 46 WHEN ic.expiresAt IS NULL THEN true 47 WHEN datetime(ic.expiresAt) >= datetime() THEN true 48 ELSE false END AS result`,
Please register or sign in to reply