Skip to content

Commit

Permalink
Merge pull request #57 from programmierbar/add-create-profile-hook
Browse files Browse the repository at this point in the history
Added create-profile extension
  • Loading branch information
Jan0707 authored Aug 13, 2024
2 parents 36849a6 + 9639752 commit f60735a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
"type": "hook",
"name": "buzzsprout",
"source": "src/buzzsprout/index.ts"
},
{
"type": "hook",
"name": "create-profile",
"source": "src/create-profile/index.ts"
}
],
"host": "^10.10.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { defineHook } from '@directus/extensions-sdk'
import { createHookErrorConstructor } from '../shared/errors.ts'
import { FilterHandler} from '@directus/types'

const HOOK_NAME = 'create-profile'

export default defineHook(({ filter }, hookContext) => {
const logger = hookContext.logger
const ItemsService = hookContext.services.ItemsService

type UserPayloadType = {
profiles: {
profiles_id: string,
}[] | undefined,
}

const handler: FilterHandler<UserPayloadType> = async function(payload, _metadata, context): Promise<UserPayloadType> {
try {
logger.info(`${HOOK_NAME} hook: Start filter function`)

if (payload.profiles && payload.profiles.length > 0) {
logger.info(`${HOOK_NAME} hook: User already has profile. Exiting early.`)
return payload
}

// Note that we manually set the knex/database connection here.
// As this called from a filter, we are within an ongoing database transaction, and the database is locked
// Meaning we need to manually use the existing connection, as a new one would not be available.
const profilesItemsService = new ItemsService('profiles', {
accountability: context.accountability,
schema: context.schema,
knex: context.database,
})

const newProfileId = await profilesItemsService.createOne({});

logger.info(`${HOOK_NAME} hook: Created profile ${newProfileId} for newly created user.`)

return {
...payload,
// the following structure is necessary for directus to make the m2m connection
profiles: [
{
profiles_id: newProfileId as string,
},
]
}

// Handle unknown errors
} catch (error: any) {
logger.error(`${HOOK_NAME} hook: Error: ${error.message}`)
const hookError = createHookErrorConstructor(HOOK_NAME, error.message)
throw new hookError()
}
}

filter('users.create', handler)
})
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ export default defineHook(({ action }, hookContext) => {
// Log start info
logger.info(`${HOOK_NAME} hook: Start "${metadata.collection}" action function`)

if (['profiles'].includes(metadata.collection)) {
logger.info(
`${HOOK_NAME} hook: Updated item was in "${metadata.collection}" collection. ` +
`Exiting hook early.`
);
return;
}

// Get fields of collection
const { fields } = context.schema.collections[metadata.collection]

Expand Down

0 comments on commit f60735a

Please sign in to comment.