diff --git a/backend/src/db/factories.js b/backend/src/db/factories.js index 1ebf063ff..ac77cdb31 100644 --- a/backend/src/db/factories.js +++ b/backend/src/db/factories.js @@ -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', diff --git a/backend/src/db/seed.js b/backend/src/db/seed.js index 685b5ef0e..f58a69e97 100644 --- a/backend/src/db/seed.js +++ b/backend/src/db/seed.js @@ -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 @jenny-rostock, what\'s up?' diff --git a/backend/src/models/InvitationCode.js b/backend/src/models/InvitationCode.js deleted file mode 100644 index 138289faf..000000000 --- a/backend/src/models/InvitationCode.js +++ /dev/null @@ -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', - }, -} diff --git a/backend/src/models/InviteCode.js b/backend/src/models/InviteCode.js index da9d3bb48..7204f1b38 100644 --- a/backend/src/models/InviteCode.js +++ b/backend/src/models/InviteCode.js @@ -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', },