factory and seeding for invite codes

This commit is contained in:
Moriz Wahl 2021-01-12 20:11:47 +01:00
parent 9449a55d42
commit 0c141b631f
4 changed files with 40 additions and 23 deletions

View File

@ -5,6 +5,7 @@ import { hashSync } from 'bcryptjs'
import { Factory } from 'rosie'
import { getDriver, getNeode } from './neo4j'
import CONFIG from '../config/index.js'
import generateInvieCode from '../schema/resolvers/helpers/generateInviteCode.js'
const neode = getNeode()
@ -216,6 +217,30 @@ Factory.define('unverifiedEmailAddress')
return neode.create('UnverifiedEmailAddress', buildObject)
})
const inviteCodeDefaults = {
code: generateInvieCode(),
createdAt: () => new Date().toISOString(),
expiresAt: () => null,
}
Factory.define('inviteCode')
.attr(inviteCodeDefaults)
.option('generatedById', null)
.option('generatedBy', ['generatedById'], (generatedById) => {
if (generatedById) return neode.find('User', generatedById)
return Factory.build('user')
})
.after(async (buildObject, options) => {
const [inviteCode, generatedBy] = await Promise.all([
neode.create('InviteCode', buildObject),
options.generatedBy,
])
await Promise.all([
inviteCode.relateTo(generatedBy, 'generated'),
])
return inviteCode
})
Factory.define('location')
.attrs({
name: 'Germany',

View File

@ -541,6 +541,16 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl']
),
])
await Factory.build(
'inviteCode',
{
code: 'AAAAAA',
},
{
generatedBy: jennyRostock,
},
)
authenticatedUser = await louie.toJson()
const mention1 =
'Hey <a class="mention" data-mention-id="u3" href="/profile/u3">@jenny-rostock</a>, what\'s up?'

View File

@ -1,16 +0,0 @@
export default {
createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() },
token: { type: 'string', primary: true, token: true },
generatedBy: {
type: 'relationship',
relationship: 'GENERATED',
target: 'User',
direction: 'in',
},
activated: {
type: 'relationship',
relationship: 'ACTIVATED',
target: 'EmailAddress',
direction: 'out',
},
}

View File

@ -1,18 +1,16 @@
export default {
code: { type: 'string', primary: true },
createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() },
uses: { type: 'int', default: () => 0 },
maxUses: { type: 'int', default: () => 1 },
active: { type: 'boolean', default: () => true },
createdBy: {
expiresAt: { type: 'string', isoDate: true, default: null },
generated: {
type: 'relationship',
relationship: 'CREATED',
relationship: 'GENERATED',
target: 'User',
direction: 'in',
},
usedBy: {
redeemed: {
type: 'relationship',
relationship: 'USED',
relationship: 'REDEEMED',
target: 'User',
direction: 'in',
},