Add emitEvents flag to item reads in service (#14164)

* Add emitEvents flag to ItemsService.read*

* Add emitEvents flag to item-read flow operation
This commit is contained in:
Rijk van Zanten
2022-06-28 17:32:01 -04:00
committed by GitHub
parent 2607037ffe
commit f971eb4f1d
4 changed files with 52 additions and 29 deletions

View File

@@ -19,6 +19,7 @@ import { PayloadService } from './payload';
export type QueryOptions = {
stripNonRequested?: boolean;
permissionsAction?: PermissionsAction;
emitEvents?: boolean;
};
export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractService {
@@ -275,36 +276,44 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
// GraphQL requires relational keys to be returned regardless
stripNonRequested: opts?.stripNonRequested !== undefined ? opts.stripNonRequested : true,
});
if (records === null) {
throw new ForbiddenException();
}
const filteredRecords = await emitter.emitFilter(
this.eventScope === 'items' ? ['items.read', `${this.collection}.items.read`] : `${this.eventScope}.read`,
records,
{
query,
collection: this.collection,
},
{
database: this.knex,
schema: this.schema,
accountability: this.accountability,
}
);
emitter.emitAction(
this.eventScope === 'items' ? ['items.read', `${this.collection}.items.read`] : `${this.eventScope}.read`,
{
payload: filteredRecords,
query,
collection: this.collection,
},
{
database: this.knex || getDatabase(),
schema: this.schema,
accountability: this.accountability,
}
);
const filteredRecords =
opts?.emitEvents !== false
? await emitter.emitFilter(
this.eventScope === 'items' ? ['items.read', `${this.collection}.items.read`] : `${this.eventScope}.read`,
records,
{
query,
collection: this.collection,
},
{
database: this.knex,
schema: this.schema,
accountability: this.accountability,
}
)
: records;
if (opts?.emitEvents !== false) {
emitter.emitAction(
this.eventScope === 'items' ? ['items.read', `${this.collection}.items.read`] : `${this.eventScope}.read`,
{
payload: filteredRecords,
query,
collection: this.collection,
},
{
database: this.knex || getDatabase(),
schema: this.schema,
accountability: this.accountability,
}
);
}
return filteredRecords as Item[];
}