Skip to content

Commit

Permalink
🚑 (audit-model) fix error while load model during builded package
Browse files Browse the repository at this point in the history
when loading Audit model with the builded adonis5-audit the package load the reference of Audit model not the instance needing call it with `.default` argument this fix, fixes it
  • Loading branch information
vinicioslc committed Jun 26, 2023
1 parent 55e4f90 commit c0da37e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/Functional/createAudit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get, omit } from 'lodash'
import { get, isNil, omit } from 'lodash'
import { RuntimeException } from 'node-exceptions'
import resolveAuditModel from '../helpers/resolveAuditModel'

Expand All @@ -18,8 +18,10 @@ export default async function ({
oldData = null,
newData = null,
}) {
const auditClass = await resolveAuditModel()

let auditClass = await resolveAuditModel()
if (!isNil(auditClass?.default)) {
auditClass = auditClass.default
}
if (!auditClass) {
throw new RuntimeException('Invalid audit model supplied ' + JSON.stringify(event))
}
Expand All @@ -35,6 +37,7 @@ export default async function ({
// get user data to store
const userId = get(auth, 'user.id', null)
// save audit
// @ts-ignore
return await auditClass.create({
userId,
auditableId: auditable_id,
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/resolveAuditModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LucidModel } from '@ioc:Adonis/Lucid/Orm'
import { isNil } from 'lodash'
import { RuntimeException } from 'node-exceptions'

export default async function () {
export default async function (): Promise<LucidModel | any> {
const Audit = (await global[Symbol.for('ioc.use')]('App/Models/Audit')) as LucidModel
if (isNil(Audit)) {
throw new RuntimeException(
Expand Down

0 comments on commit c0da37e

Please sign in to comment.