feat: create ViewField model (#961)
* feat: create ViewField model - Created ViewField prisma model - Added ViewField server resolvers for findMany/updateOne - Added getViewFields/updateViewField graphql queries Closes #849 * chore: update node version in .nvmrc files
This commit is contained in:
56
server/src/ability/handlers/view-field.ability-handler.ts
Normal file
56
server/src/ability/handlers/view-field.ability-handler.ts
Normal file
@ -0,0 +1,56 @@
|
||||
import {
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { GqlExecutionContext } from '@nestjs/graphql';
|
||||
|
||||
import { subject } from '@casl/ability';
|
||||
|
||||
import { IAbilityHandler } from 'src/ability/interfaces/ability-handler.interface';
|
||||
|
||||
import { AbilityAction } from 'src/ability/ability.action';
|
||||
import { AppAbility } from 'src/ability/ability.factory';
|
||||
import { relationAbilityChecker } from 'src/ability/ability.util';
|
||||
import { ViewFieldWhereInput } from 'src/core/@generated/view-field/view-field-where.input';
|
||||
import { PrismaService } from 'src/database/prisma.service';
|
||||
import { assert } from 'src/utils/assert';
|
||||
|
||||
class ViewFieldArgs {
|
||||
where?: ViewFieldWhereInput;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ReadViewFieldAbilityHandler implements IAbilityHandler {
|
||||
handle(ability: AppAbility) {
|
||||
return ability.can(AbilityAction.Read, 'ViewField');
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class UpdateViewFieldAbilityHandler implements IAbilityHandler {
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
|
||||
async handle(ability: AppAbility, context: ExecutionContext) {
|
||||
const gqlContext = GqlExecutionContext.create(context);
|
||||
const args = gqlContext.getArgs<ViewFieldArgs>();
|
||||
const viewField = await this.prismaService.client.viewField.findFirst({
|
||||
where: args.where,
|
||||
});
|
||||
assert(viewField, '', NotFoundException);
|
||||
|
||||
const allowed = await relationAbilityChecker(
|
||||
'ViewField',
|
||||
ability,
|
||||
this.prismaService.client,
|
||||
args,
|
||||
);
|
||||
|
||||
if (!allowed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ability.can(AbilityAction.Update, subject('ViewField', viewField));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user