Skip to content
Snippets Groups Projects

feat: Invite codes

Merged Hannes Heine requested to merge invite-codes into master
2 unresolved threads

Created by: Mogge

:cake: Pullrequest

Backend part for using invite codes for user registration

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
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)
  • Hannes Heine
  • Hannes Heine
    Hannes Heine @Elweyn started a thread on commit c6ff0723
  • 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 })
  • Created by: ulfgebhardt

    Review: Dismissed

    Awesome work! Just a few pit falls.

    • Ascii encoding incorrect?
    • Lower caps Codes acceptance?
  • Hannes Heine
  • Hannes Heine
  • Hannes Heine
    Hannes Heine @Elweyn started a thread on commit a9817048
  • 205 206 }
    206 207
    207 208 Factory.define('emailAddress')
    208 .attr(emailDefaults)
    209 .attrs(emailDefaults)
  • Hannes Heine
  • Hannes Heine
    Hannes Heine @Elweyn started a thread on commit a9817048
  • 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`,
    • Created by: Tirokk

      Love that you uses properties here instead of getting them later :heart_eyes: Very efficient coding! Smart!

  • Hannes Heine
    Hannes Heine @Elweyn started a thread on commit a9817048
  • 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`,
  • Hannes Heine
  • Hannes Heine
  • Created by: Tirokk

    Review: Approved

    Yeah @Mogge This is an important peace of work to reach our GO LIVE! :rocket::rocket::dizzy:

    Great and sustainable work you have done. I suggested just some easy changes and asked some not as important questions …

    Wisdom

  • Merged by: Mogge at 2021-01-20 11:47:12 UTC

  • Please register or sign in to reply
    Loading