@ -17,8 +17,8 @@ import {
|
||||
PipelineStage,
|
||||
PipelineProgress,
|
||||
UserSettings,
|
||||
ViewField,
|
||||
View,
|
||||
ViewField,
|
||||
ViewSort,
|
||||
} from '@prisma/client';
|
||||
|
||||
@ -134,11 +134,22 @@ export class AbilityFactory {
|
||||
workspaceId: workspace.id,
|
||||
});
|
||||
|
||||
// View
|
||||
can(AbilityAction.Read, 'View', { workspaceId: workspace.id });
|
||||
can(AbilityAction.Create, 'View', { workspaceId: workspace.id });
|
||||
can(AbilityAction.Update, 'View', { workspaceId: workspace.id });
|
||||
|
||||
// ViewField
|
||||
can(AbilityAction.Read, 'ViewField', { workspaceId: workspace.id });
|
||||
can(AbilityAction.Create, 'ViewField', { workspaceId: workspace.id });
|
||||
can(AbilityAction.Update, 'ViewField', { workspaceId: workspace.id });
|
||||
|
||||
// ViewSort
|
||||
can(AbilityAction.Read, 'ViewSort', { workspaceId: workspace.id });
|
||||
can(AbilityAction.Create, 'ViewSort', { workspaceId: workspace.id });
|
||||
can(AbilityAction.Update, 'ViewSort', { workspaceId: workspace.id });
|
||||
can(AbilityAction.Delete, 'ViewSort', { workspaceId: workspace.id });
|
||||
|
||||
return build();
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,6 +99,12 @@ import {
|
||||
ReadViewFieldAbilityHandler,
|
||||
UpdateViewFieldAbilityHandler,
|
||||
} from './handlers/view-field.ability-handler';
|
||||
import {
|
||||
CreateViewSortAbilityHandler,
|
||||
ReadViewSortAbilityHandler,
|
||||
UpdateViewSortAbilityHandler,
|
||||
DeleteViewSortAbilityHandler,
|
||||
} from './handlers/view-sort.ability-handler';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
@ -187,6 +193,11 @@ import {
|
||||
ReadViewFieldAbilityHandler,
|
||||
CreateViewFieldAbilityHandler,
|
||||
UpdateViewFieldAbilityHandler,
|
||||
// ViewSort
|
||||
ReadViewSortAbilityHandler,
|
||||
CreateViewSortAbilityHandler,
|
||||
UpdateViewSortAbilityHandler,
|
||||
DeleteViewSortAbilityHandler,
|
||||
],
|
||||
exports: [
|
||||
AbilityFactory,
|
||||
@ -272,6 +283,11 @@ import {
|
||||
ReadViewFieldAbilityHandler,
|
||||
CreateViewFieldAbilityHandler,
|
||||
UpdateViewFieldAbilityHandler,
|
||||
// ViewSort
|
||||
ReadViewSortAbilityHandler,
|
||||
CreateViewSortAbilityHandler,
|
||||
UpdateViewSortAbilityHandler,
|
||||
DeleteViewSortAbilityHandler,
|
||||
],
|
||||
})
|
||||
export class AbilityModule {}
|
||||
|
||||
122
server/src/ability/handlers/view-sort.ability-handler.ts
Normal file
122
server/src/ability/handlers/view-sort.ability-handler.ts
Normal file
@ -0,0 +1,122 @@
|
||||
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 {
|
||||
convertToWhereInput,
|
||||
relationAbilityChecker,
|
||||
} from 'src/ability/ability.util';
|
||||
import { PrismaService } from 'src/database/prisma.service';
|
||||
import { assert } from 'src/utils/assert';
|
||||
import { ViewSortWhereUniqueInput } from 'src/core/@generated/view-sort/view-sort-where-unique.input';
|
||||
import { ViewSortWhereInput } from 'src/core/@generated/view-sort/view-sort-where.input';
|
||||
|
||||
class ViewSortArgs {
|
||||
where?: ViewSortWhereInput | ViewSortWhereUniqueInput;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
const isViewSortWhereUniqueInput = (
|
||||
input: ViewSortWhereInput | ViewSortWhereUniqueInput,
|
||||
): input is ViewSortWhereUniqueInput => 'viewId_key' in input;
|
||||
|
||||
@Injectable()
|
||||
export class ReadViewSortAbilityHandler implements IAbilityHandler {
|
||||
handle(ability: AppAbility) {
|
||||
return ability.can(AbilityAction.Read, 'ViewSort');
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class CreateViewSortAbilityHandler implements IAbilityHandler {
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
|
||||
async handle(ability: AppAbility, context: ExecutionContext) {
|
||||
const gqlContext = GqlExecutionContext.create(context);
|
||||
const args = gqlContext.getArgs();
|
||||
|
||||
const allowed = await relationAbilityChecker(
|
||||
'ViewSort',
|
||||
ability,
|
||||
this.prismaService.client,
|
||||
args,
|
||||
);
|
||||
|
||||
if (!allowed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ability.can(AbilityAction.Create, 'ViewSort');
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class UpdateViewSortAbilityHandler implements IAbilityHandler {
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
|
||||
async handle(ability: AppAbility, context: ExecutionContext) {
|
||||
const gqlContext = GqlExecutionContext.create(context);
|
||||
const args = gqlContext.getArgs<ViewSortArgs>();
|
||||
const viewSort = await this.prismaService.client.viewSort.findFirst({
|
||||
where:
|
||||
args.where && isViewSortWhereUniqueInput(args.where)
|
||||
? args.where.viewId_key
|
||||
: args.where,
|
||||
});
|
||||
assert(viewSort, '', NotFoundException);
|
||||
|
||||
const allowed = await relationAbilityChecker(
|
||||
'ViewSort',
|
||||
ability,
|
||||
this.prismaService.client,
|
||||
args,
|
||||
);
|
||||
|
||||
if (!allowed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ability.can(AbilityAction.Update, subject('ViewSort', viewSort));
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class DeleteViewSortAbilityHandler implements IAbilityHandler {
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
|
||||
async handle(ability: AppAbility, context: ExecutionContext) {
|
||||
const gqlContext = GqlExecutionContext.create(context);
|
||||
const args = gqlContext.getArgs<ViewSortArgs>();
|
||||
const where = convertToWhereInput(
|
||||
args.where && isViewSortWhereUniqueInput(args.where)
|
||||
? args.where.viewId_key
|
||||
: args.where,
|
||||
);
|
||||
const viewSorts = await this.prismaService.client.viewSort.findMany({
|
||||
where,
|
||||
});
|
||||
assert(viewSorts.length, '', NotFoundException);
|
||||
|
||||
for (const viewSort of viewSorts) {
|
||||
const allowed = ability.can(
|
||||
AbilityAction.Delete,
|
||||
subject('ViewSort', viewSort),
|
||||
);
|
||||
|
||||
if (!allowed) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
79
server/src/ability/handlers/view.ability-handler.ts
Normal file
79
server/src/ability/handlers/view.ability-handler.ts
Normal file
@ -0,0 +1,79 @@
|
||||
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 { ViewWhereInput } from 'src/core/@generated/view/view-where.input';
|
||||
import { PrismaService } from 'src/database/prisma.service';
|
||||
import { assert } from 'src/utils/assert';
|
||||
|
||||
class ViewArgs {
|
||||
where?: ViewWhereInput;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ReadViewAbilityHandler implements IAbilityHandler {
|
||||
handle(ability: AppAbility) {
|
||||
return ability.can(AbilityAction.Read, 'View');
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class CreateViewAbilityHandler implements IAbilityHandler {
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
|
||||
async handle(ability: AppAbility, context: ExecutionContext) {
|
||||
const gqlContext = GqlExecutionContext.create(context);
|
||||
const args = gqlContext.getArgs();
|
||||
|
||||
const allowed = await relationAbilityChecker(
|
||||
'View',
|
||||
ability,
|
||||
this.prismaService.client,
|
||||
args,
|
||||
);
|
||||
|
||||
if (!allowed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ability.can(AbilityAction.Create, 'View');
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class UpdateViewAbilityHandler implements IAbilityHandler {
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
|
||||
async handle(ability: AppAbility, context: ExecutionContext) {
|
||||
const gqlContext = GqlExecutionContext.create(context);
|
||||
const args = gqlContext.getArgs<ViewArgs>();
|
||||
const view = await this.prismaService.client.view.findFirst({
|
||||
where: args.where,
|
||||
});
|
||||
assert(view, '', NotFoundException);
|
||||
|
||||
const allowed = await relationAbilityChecker(
|
||||
'View',
|
||||
ability,
|
||||
this.prismaService.client,
|
||||
args,
|
||||
);
|
||||
|
||||
if (!allowed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ability.can(AbilityAction.Update, subject('View', view));
|
||||
}
|
||||
}
|
||||
32
server/src/core/view/resolvers/view-sort.resolver.spec.ts
Normal file
32
server/src/core/view/resolvers/view-sort.resolver.spec.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
|
||||
import { ViewSortService } from 'src/core/view/services/view-sort.service';
|
||||
import { AbilityFactory } from 'src/ability/ability.factory';
|
||||
|
||||
import { ViewSortResolver } from './view-sort.resolver';
|
||||
|
||||
describe('ViewSortResolver', () => {
|
||||
let resolver: ViewSortResolver;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
ViewSortResolver,
|
||||
{
|
||||
provide: ViewSortService,
|
||||
useValue: {},
|
||||
},
|
||||
{
|
||||
provide: AbilityFactory,
|
||||
useValue: {},
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
resolver = module.get<ViewSortResolver>(ViewSortResolver);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(resolver).toBeDefined();
|
||||
});
|
||||
});
|
||||
102
server/src/core/view/resolvers/view-sort.resolver.ts
Normal file
102
server/src/core/view/resolvers/view-sort.resolver.ts
Normal file
@ -0,0 +1,102 @@
|
||||
import { UseGuards } from '@nestjs/common';
|
||||
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
|
||||
|
||||
import { accessibleBy } from '@casl/prisma';
|
||||
import { Prisma, Workspace } from '@prisma/client';
|
||||
|
||||
import { AppAbility } from 'src/ability/ability.factory';
|
||||
import {
|
||||
CreateViewSortAbilityHandler,
|
||||
DeleteViewSortAbilityHandler,
|
||||
ReadViewSortAbilityHandler,
|
||||
UpdateViewSortAbilityHandler,
|
||||
} from 'src/ability/handlers/view-sort.ability-handler';
|
||||
import { FindManyViewSortArgs } from 'src/core/@generated/view-sort/find-many-view-sort.args';
|
||||
import { ViewSort } from 'src/core/@generated/view-sort/view-sort.model';
|
||||
import { ViewSortService } from 'src/core/view/services/view-sort.service';
|
||||
import { CheckAbilities } from 'src/decorators/check-abilities.decorator';
|
||||
import {
|
||||
PrismaSelect,
|
||||
PrismaSelector,
|
||||
} from 'src/decorators/prisma-select.decorator';
|
||||
import { UserAbility } from 'src/decorators/user-ability.decorator';
|
||||
import { AbilityGuard } from 'src/guards/ability.guard';
|
||||
import { JwtAuthGuard } from 'src/guards/jwt.auth.guard';
|
||||
import { UpdateOneViewSortArgs } from 'src/core/@generated/view-sort/update-one-view-sort.args';
|
||||
import { AuthWorkspace } from 'src/decorators/auth-workspace.decorator';
|
||||
import { AffectedRows } from 'src/core/@generated/prisma/affected-rows.output';
|
||||
import { DeleteManyViewSortArgs } from 'src/core/@generated/view-sort/delete-many-view-sort.args';
|
||||
import { CreateManyViewSortArgs } from 'src/core/@generated/view-sort/create-many-view-sort.args';
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Resolver(() => ViewSort)
|
||||
export class ViewSortResolver {
|
||||
constructor(private readonly viewSortService: ViewSortService) {}
|
||||
|
||||
@Mutation(() => AffectedRows)
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(CreateViewSortAbilityHandler)
|
||||
async createManyViewSort(
|
||||
@Args() args: CreateManyViewSortArgs,
|
||||
@AuthWorkspace() workspace: Workspace,
|
||||
): Promise<AffectedRows> {
|
||||
return this.viewSortService.createMany({
|
||||
data: args.data.map((data) => ({
|
||||
...data,
|
||||
workspaceId: workspace.id,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
@Query(() => [ViewSort])
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(ReadViewSortAbilityHandler)
|
||||
async findManyViewSort(
|
||||
@Args() args: FindManyViewSortArgs,
|
||||
@UserAbility() ability: AppAbility,
|
||||
@PrismaSelector({ modelName: 'ViewSort' })
|
||||
prismaSelect: PrismaSelect<'ViewSort'>,
|
||||
): Promise<Partial<ViewSort>[]> {
|
||||
return this.viewSortService.findMany({
|
||||
where: args.where
|
||||
? {
|
||||
AND: [args.where, accessibleBy(ability).ViewSort],
|
||||
}
|
||||
: accessibleBy(ability).ViewSort,
|
||||
orderBy: args.orderBy,
|
||||
cursor: args.cursor,
|
||||
take: args.take,
|
||||
skip: args.skip,
|
||||
distinct: args.distinct,
|
||||
select: prismaSelect.value,
|
||||
});
|
||||
}
|
||||
|
||||
@Mutation(() => ViewSort)
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(UpdateViewSortAbilityHandler)
|
||||
async updateOneViewSort(
|
||||
@Args() args: UpdateOneViewSortArgs,
|
||||
@PrismaSelector({ modelName: 'ViewSort' })
|
||||
prismaSelect: PrismaSelect<'ViewSort'>,
|
||||
): Promise<Partial<ViewSort>> {
|
||||
return this.viewSortService.update({
|
||||
data: args.data,
|
||||
where: args.where,
|
||||
select: prismaSelect.value,
|
||||
} as Prisma.ViewSortUpdateArgs);
|
||||
}
|
||||
|
||||
@Mutation(() => AffectedRows, {
|
||||
nullable: false,
|
||||
})
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(DeleteViewSortAbilityHandler)
|
||||
async deleteManyViewSort(
|
||||
@Args() args: DeleteManyViewSortArgs,
|
||||
): Promise<AffectedRows> {
|
||||
return this.viewSortService.deleteMany({
|
||||
where: args.where,
|
||||
});
|
||||
}
|
||||
}
|
||||
28
server/src/core/view/services/view-sort.service.spec.ts
Normal file
28
server/src/core/view/services/view-sort.service.spec.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
|
||||
import { PrismaService } from 'src/database/prisma.service';
|
||||
import { prismaMock } from 'src/database/client-mock/jest-prisma-singleton';
|
||||
|
||||
import { ViewSortService } from './view-sort.service';
|
||||
|
||||
describe('ViewSortService', () => {
|
||||
let service: ViewSortService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
ViewSortService,
|
||||
{
|
||||
provide: PrismaService,
|
||||
useValue: prismaMock,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<ViewSortService>(ViewSortService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
39
server/src/core/view/services/view-sort.service.ts
Normal file
39
server/src/core/view/services/view-sort.service.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { PrismaService } from 'src/database/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class ViewSortService {
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
|
||||
// Find
|
||||
findFirst = this.prismaService.client.viewSort.findFirst;
|
||||
findFirstOrThrow = this.prismaService.client.viewSort.findFirstOrThrow;
|
||||
|
||||
findUnique = this.prismaService.client.viewSort.findUnique;
|
||||
findUniqueOrThrow = this.prismaService.client.viewSort.findUniqueOrThrow;
|
||||
|
||||
findMany = this.prismaService.client.viewSort.findMany;
|
||||
|
||||
// Create
|
||||
create = this.prismaService.client.viewSort.create;
|
||||
createMany = this.prismaService.client.viewSort.createMany;
|
||||
|
||||
// Update
|
||||
update = this.prismaService.client.viewSort.update;
|
||||
upsert = this.prismaService.client.viewSort.upsert;
|
||||
updateMany = this.prismaService.client.viewSort.updateMany;
|
||||
|
||||
// Delete
|
||||
delete = this.prismaService.client.viewSort.delete;
|
||||
deleteMany = this.prismaService.client.viewSort.deleteMany;
|
||||
|
||||
// Aggregate
|
||||
aggregate = this.prismaService.client.viewSort.aggregate;
|
||||
|
||||
// Count
|
||||
count = this.prismaService.client.viewSort.count;
|
||||
|
||||
// GroupBy
|
||||
groupBy = this.prismaService.client.viewSort.groupBy;
|
||||
}
|
||||
@ -2,8 +2,15 @@ import { Module } from '@nestjs/common';
|
||||
|
||||
import { ViewFieldService } from './services/view-field.service';
|
||||
import { ViewFieldResolver } from './resolvers/view-field.resolver';
|
||||
import { ViewSortService } from './services/view-sort.service';
|
||||
import { ViewSortResolver } from './resolvers/view-sort.resolver';
|
||||
|
||||
@Module({
|
||||
providers: [ViewFieldService, ViewFieldResolver],
|
||||
providers: [
|
||||
ViewFieldService,
|
||||
ViewSortService,
|
||||
ViewFieldResolver,
|
||||
ViewSortResolver,
|
||||
],
|
||||
})
|
||||
export class ViewModule {}
|
||||
|
||||
Reference in New Issue
Block a user