feat: Favorites (#1094)
* Adding the favorite button * favorites services and resolvers * favorites schema * favorite ability handler * favorite module export * front end UI * front end graphql additions * server ability handlers * server resolvers and services * css fix * Adding the favorite button * favorites services and resolvers * favorites schema * favorite ability handler * favorite module export * front end UI * front end graphql additions * server ability handlers * server resolvers and services * css fix * delete favorites handler and resolver * removed favorite from index list * chip avatar size props * index list additions * UI additions for favorites functionality * lint fixes * graphql codegen * UI fixes * favorite hook addition * moved to ~/modules * Favorite mapping to workspaceMember * graphql codegen * cosmetic changes * camel cased methods * graphql codegen
This commit is contained in:
74
server/src/ability/handlers/favorite.ability-handler.ts
Normal file
74
server/src/ability/handlers/favorite.ability-handler.ts
Normal file
@ -0,0 +1,74 @@
|
||||
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 { PrismaService } from 'src/database/prisma.service';
|
||||
import { AbilityAction } from 'src/ability/ability.action';
|
||||
import { AppAbility } from 'src/ability/ability.factory';
|
||||
import { relationAbilityChecker } from 'src/ability/ability.util';
|
||||
import { FavoriteWhereInput } from 'src/core/@generated/favorite/favorite-where.input';
|
||||
import { assert } from 'src/utils/assert';
|
||||
|
||||
class FavoriteArgs {
|
||||
where?: FavoriteWhereInput;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ManageFavoriteAbilityHandler implements IAbilityHandler {
|
||||
async handle(ability: AppAbility) {
|
||||
return ability.can(AbilityAction.Manage, 'Favorite');
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ReadFavoriteAbilityHandler implements IAbilityHandler {
|
||||
handle(ability: AppAbility) {
|
||||
return ability.can(AbilityAction.Read, 'Favorite');
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class CreateFavoriteAbilityHandler 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(
|
||||
'Favorite',
|
||||
ability,
|
||||
this.prismaService.client,
|
||||
args,
|
||||
);
|
||||
|
||||
if (!allowed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ability.can(AbilityAction.Create, 'Favorite');
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class DeleteFavoriteAbilityHandler implements IAbilityHandler {
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
|
||||
async handle(ability: AppAbility, context: ExecutionContext) {
|
||||
const gqlContext = GqlExecutionContext.create(context);
|
||||
const args = gqlContext.getArgs<FavoriteArgs>();
|
||||
const favorite = await this.prismaService.client.favorite.findFirst({
|
||||
where: args.where,
|
||||
});
|
||||
assert(favorite, '', NotFoundException);
|
||||
|
||||
return ability.can(AbilityAction.Delete, subject('Favorite', favorite));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user