-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from programmierbar/add-create-profile-hook
Added create-profile extension
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
directus-cms/extensions/directus-extension-programmierbar-bundle/src/create-profile/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters