Skip to content

Commit

Permalink
feat(cms): add support for invite code registration'
Browse files Browse the repository at this point in the history
  • Loading branch information
bahdcoder committed Jan 4, 2022
1 parent 632e4c0 commit 52e7536
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 44 deletions.
4 changes: 3 additions & 1 deletion examples/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { graphql } from '@tensei/graphql'
import { files, media } from '@tensei/media'
import { jsonPlugin } from '@tensei/field-json'

import { seed } from './seed'

import {
tensei,
welcome,
Expand Down Expand Up @@ -145,7 +147,7 @@ export default tensei()
dbName: 'db.sqlite'
})
.boot(async ({ repositories }) => {
// await seed(repositories)
await seed(repositories)
console.log('App running on http://localhost:8810')
})
.start()
Expand Down
22 changes: 20 additions & 2 deletions examples/typescript/src/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ export function order() {
}
}

export function adminUser() {
return {
firstName: Faker.name.firstName(),
lastName: Faker.name.lastName(),
email: Faker.internet.exampleEmail(),
password: 'password',
active: true
}
}

export function customer() {
return {
email: Faker.internet.exampleEmail(),
Expand All @@ -56,6 +66,11 @@ export function generate(fn: () => any, length = 20) {
}

export async function seed(db: any) {
if (!process.env.TENSEI_SEED) {
return
}

const adminUsers = generate(adminUser).map(u => db.adminUsers().create(u))
const customers = generate(customer).map((c: any) => db.customers().create(c))
const orders = generate(order)
.map((o: any) => db.orders().create(o))
Expand Down Expand Up @@ -83,6 +98,9 @@ export async function seed(db: any) {
return order
})

await db.orders().persistAndFlush(orders)
await db.products().persistAndFlush(products)
await Promise.all([
db.orders().persistAndFlush(orders),
db.products().persistAndFlush(products),
db.adminUsers().persistAndFlush(adminUsers)
])
}
Original file line number Diff line number Diff line change
Expand Up @@ -426,13 +426,7 @@ const RolesTable: React.FC = () => {
<>
<TableHeading>
<EuiTitle>
<h1>
{adminRoles.length > 1
? `${adminRoles.length} existing roles`
: adminRoles.length === 1
? `1 existing role`
: `No roles`}
</h1>
<h1>All roles ({adminRoles.length})</h1>
</EuiTitle>
<EuiButtonEmpty
onClick={() => {
Expand Down
98 changes: 64 additions & 34 deletions packages/cms/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ class CmsPlugin {
roleResource: string
dashboards: DashboardContract[]
} = {
path: 'cms',
apiPath: 'cms/api',
setup: () => { },
cookieOptions: {},
dashboards: [],
userResource: 'Admin User',
permissionResource: 'Admin Permission',
roleResource: 'Admin Role',
tokenResource: 'Admin Token'
}
path: 'cms',
apiPath: 'cms/api',
setup: () => {},
cookieOptions: {},
dashboards: [],
userResource: 'Admin User',
permissionResource: 'Admin Permission',
roleResource: 'Admin Role',
tokenResource: 'Admin Token'
}

private router = Router()

Expand Down Expand Up @@ -156,7 +156,8 @@ class CmsPlugin {
let user: any = await manager.findOne(
this.resources.user.data.pascalCaseName,
{
email
email,
active: true
}
)

Expand Down Expand Up @@ -192,8 +193,36 @@ class CmsPlugin {

private registerPassport = async (request: Request, done: any) => {
const { config, manager, body, resources, repositories } = request

const { emitter } = config

let invitedUser = null

if (body.inviteCode) {
invitedUser = await manager.findOne(
this.resources.user.data.pascalCaseName,
{
inviteCode: body.inviteCode
}
)
}

if (invitedUser) {
manager.assign(invitedUser, {
active: true,
inviteCode: null,
password: body.password,
firstName: body.firstName ?? invitedUser.firstName,
lastName: body.lastName ?? invitedUser.lastName
})

await manager.persistAndFlush(invitedUser)

emitter.emit('ADMIN_REGISTERED', invitedUser)

return done(null, invitedUser)
}

const adminCount = await manager.count(
this.resources.user.data.pascalCaseName,
{}
Expand Down Expand Up @@ -339,6 +368,7 @@ class CmsPlugin {
.defaultFormValue(true)
.default(true)
.rules('boolean'),
text('Invite code').nullable(),
belongsToMany(this.config.roleResource).rules('array')
])
.displayField('First name')
Expand Down Expand Up @@ -585,26 +615,26 @@ class CmsPlugin {
})

this.router.use(Csurf())
;[...getRoutes(config, this.config), ...this.routes()].forEach(
route => {
const path = route.config.path.startsWith('/')
? route.config.path
: `/${route.config.path}`
; (this.router as any)[route.config.type.toLowerCase()](
path,

...route.config.middleware.map(fn => AsyncHandler(fn)),
AsyncHandler(async (request, response, next) => {
await this.authorizeResolver(request as any, route)

return next()
}),
AsyncHandler(async (request, response) =>
route.config.handler(request, response)
)
)
}
)
;[...getRoutes(config, this.config), ...this.routes()].forEach(
route => {
const path = route.config.path.startsWith('/')
? route.config.path
: `/${route.config.path}`
;(this.router as any)[route.config.type.toLowerCase()](
path,

...route.config.middleware.map(fn => AsyncHandler(fn)),
AsyncHandler(async (request, response, next) => {
await this.authorizeResolver(request as any, route)

return next()
}),
AsyncHandler(async (request, response) =>
route.config.handler(request, response)
)
)
}
)

app.use(`/${this.config.path}`, this.router)

Expand All @@ -620,9 +650,9 @@ class CmsPlugin {
// @ts-ignore
user: request.user
? JSON.stringify({
// @ts-ignore
...request.user
})
// @ts-ignore
...request.user
})
: null,
resources: JSON.stringify(
request.config.resources.map(r => r.serialize())
Expand Down

0 comments on commit 52e7536

Please sign in to comment.