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:
@ -19,6 +19,7 @@ import {
|
||||
UserSettings,
|
||||
View,
|
||||
ViewField,
|
||||
Favorite,
|
||||
ViewSort,
|
||||
} from '@prisma/client';
|
||||
|
||||
@ -41,6 +42,7 @@ type SubjectsAbility = Subjects<{
|
||||
UserSettings: UserSettings;
|
||||
View: View;
|
||||
ViewField: ViewField;
|
||||
Favorite: Favorite;
|
||||
ViewSort: ViewSort;
|
||||
}>;
|
||||
|
||||
@ -143,6 +145,12 @@ export class AbilityFactory {
|
||||
can(AbilityAction.Read, 'ViewField', { workspaceId: workspace.id });
|
||||
can(AbilityAction.Create, 'ViewField', { workspaceId: workspace.id });
|
||||
can(AbilityAction.Update, 'ViewField', { workspaceId: workspace.id });
|
||||
//Favorite
|
||||
can(AbilityAction.Read, 'Favorite', { workspaceId: workspace.id });
|
||||
can(AbilityAction.Create, 'Favorite');
|
||||
can(AbilityAction.Delete, 'Favorite', {
|
||||
workspaceId: workspace.id,
|
||||
});
|
||||
|
||||
// ViewSort
|
||||
can(AbilityAction.Read, 'ViewSort', { workspaceId: workspace.id });
|
||||
|
||||
@ -99,6 +99,11 @@ import {
|
||||
ReadViewFieldAbilityHandler,
|
||||
UpdateViewFieldAbilityHandler,
|
||||
} from './handlers/view-field.ability-handler';
|
||||
import {
|
||||
CreateFavoriteAbilityHandler,
|
||||
ReadFavoriteAbilityHandler,
|
||||
DeleteFavoriteAbilityHandler,
|
||||
} from './handlers/favorite.ability-handler';
|
||||
import {
|
||||
CreateViewSortAbilityHandler,
|
||||
ReadViewSortAbilityHandler,
|
||||
@ -193,6 +198,10 @@ import {
|
||||
ReadViewFieldAbilityHandler,
|
||||
CreateViewFieldAbilityHandler,
|
||||
UpdateViewFieldAbilityHandler,
|
||||
//Favorite
|
||||
ReadFavoriteAbilityHandler,
|
||||
CreateFavoriteAbilityHandler,
|
||||
DeleteFavoriteAbilityHandler,
|
||||
// ViewSort
|
||||
ReadViewSortAbilityHandler,
|
||||
CreateViewSortAbilityHandler,
|
||||
@ -283,6 +292,10 @@ import {
|
||||
ReadViewFieldAbilityHandler,
|
||||
CreateViewFieldAbilityHandler,
|
||||
UpdateViewFieldAbilityHandler,
|
||||
//Favorite
|
||||
ReadFavoriteAbilityHandler,
|
||||
CreateFavoriteAbilityHandler,
|
||||
DeleteFavoriteAbilityHandler,
|
||||
// ViewSort
|
||||
ReadViewSortAbilityHandler,
|
||||
CreateViewSortAbilityHandler,
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
@ -13,6 +13,7 @@ import { ClientConfigModule } from './client-config/client-config.module';
|
||||
import { AttachmentModule } from './attachment/attachment.module';
|
||||
import { ActivityModule } from './activity/activity.module';
|
||||
import { ViewModule } from './view/view.module';
|
||||
import { FavoriteModule } from './favorite/favorite.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -29,6 +30,7 @@ import { ViewModule } from './view/view.module';
|
||||
AttachmentModule,
|
||||
ActivityModule,
|
||||
ViewModule,
|
||||
FavoriteModule,
|
||||
],
|
||||
exports: [
|
||||
AuthModule,
|
||||
@ -40,6 +42,7 @@ import { ViewModule } from './view/view.module';
|
||||
WorkspaceModule,
|
||||
AnalyticsModule,
|
||||
AttachmentModule,
|
||||
FavoriteModule,
|
||||
],
|
||||
})
|
||||
export class CoreModule {}
|
||||
|
||||
10
server/src/core/favorite/favorite.module.ts
Normal file
10
server/src/core/favorite/favorite.module.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { FavoriteResolver } from './resolvers/favorite.resolver';
|
||||
import { FavoriteService } from './services/favorite.service';
|
||||
|
||||
@Module({
|
||||
providers: [FavoriteService, FavoriteResolver],
|
||||
exports: [FavoriteService],
|
||||
})
|
||||
export class FavoriteModule {}
|
||||
143
server/src/core/favorite/resolvers/favorite.resolver.ts
Normal file
143
server/src/core/favorite/resolvers/favorite.resolver.ts
Normal file
@ -0,0 +1,143 @@
|
||||
import { Resolver, Query, Args, Mutation } from '@nestjs/graphql';
|
||||
import { UseGuards } from '@nestjs/common';
|
||||
import { InputType } from '@nestjs/graphql';
|
||||
import { Field } from '@nestjs/graphql';
|
||||
|
||||
import { Workspace } from '@prisma/client';
|
||||
|
||||
import {
|
||||
PrismaSelect,
|
||||
PrismaSelector,
|
||||
} from 'src/decorators/prisma-select.decorator';
|
||||
import { JwtAuthGuard } from 'src/guards/jwt.auth.guard';
|
||||
import { Favorite } from 'src/core/@generated/favorite/favorite.model';
|
||||
import { AbilityGuard } from 'src/guards/ability.guard';
|
||||
import { CheckAbilities } from 'src/decorators/check-abilities.decorator';
|
||||
import {
|
||||
CreateFavoriteAbilityHandler,
|
||||
DeleteFavoriteAbilityHandler,
|
||||
ReadFavoriteAbilityHandler,
|
||||
} from 'src/ability/handlers/favorite.ability-handler';
|
||||
import { AuthWorkspace } from 'src/decorators/auth-workspace.decorator';
|
||||
import { FavoriteService } from 'src/core/favorite/services/favorite.service';
|
||||
import { FavoriteWhereInput } from 'src/core/@generated/favorite/favorite-where.input';
|
||||
|
||||
@InputType()
|
||||
class FavoriteMutationForPersonArgs {
|
||||
@Field(() => String)
|
||||
personId: string;
|
||||
}
|
||||
|
||||
@InputType()
|
||||
class FavoriteMutationForCompanyArgs {
|
||||
@Field(() => String)
|
||||
companyId: string;
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Resolver(() => Favorite)
|
||||
export class FavoriteResolver {
|
||||
constructor(private readonly favoriteService: FavoriteService) {}
|
||||
|
||||
@Query(() => [Favorite])
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(ReadFavoriteAbilityHandler)
|
||||
async findFavorites(
|
||||
@AuthWorkspace() workspace: Workspace,
|
||||
): Promise<Partial<Favorite>[]> {
|
||||
const favorites = await this.favoriteService.findMany({
|
||||
where: {
|
||||
workspaceId: workspace.id,
|
||||
},
|
||||
include: {
|
||||
person: true,
|
||||
company: {
|
||||
include: {
|
||||
accountOwner: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return favorites;
|
||||
}
|
||||
|
||||
@Mutation(() => Favorite, {
|
||||
nullable: false,
|
||||
})
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(CreateFavoriteAbilityHandler)
|
||||
async createFavoriteForPerson(
|
||||
@Args('data') args: FavoriteMutationForPersonArgs,
|
||||
@AuthWorkspace() workspace: Workspace,
|
||||
@PrismaSelector({ modelName: 'Favorite' })
|
||||
prismaSelect: PrismaSelect<'Favorite'>,
|
||||
): Promise<Partial<Favorite>> {
|
||||
//To avoid duplicates we first fetch all favorites assinged by workspace
|
||||
const favorite = await this.favoriteService.findFirst({
|
||||
where: { workspaceId: workspace.id, personId: args.personId },
|
||||
});
|
||||
|
||||
if (favorite) return favorite;
|
||||
|
||||
return this.favoriteService.create({
|
||||
data: {
|
||||
person: {
|
||||
connect: { id: args.personId },
|
||||
},
|
||||
workspaceId: workspace.id,
|
||||
},
|
||||
select: prismaSelect.value,
|
||||
});
|
||||
}
|
||||
|
||||
@Mutation(() => Favorite, {
|
||||
nullable: false,
|
||||
})
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(CreateFavoriteAbilityHandler)
|
||||
async createFavoriteForCompany(
|
||||
@Args('data') args: FavoriteMutationForCompanyArgs,
|
||||
@AuthWorkspace() workspace: Workspace,
|
||||
@PrismaSelector({ modelName: 'Favorite' })
|
||||
prismaSelect: PrismaSelect<'Favorite'>,
|
||||
): Promise<Partial<Favorite>> {
|
||||
//To avoid duplicates we first fetch all favorites assinged by workspace
|
||||
const favorite = await this.favoriteService.findFirst({
|
||||
where: { workspaceId: workspace.id, companyId: args.companyId },
|
||||
});
|
||||
|
||||
if (favorite) return favorite;
|
||||
|
||||
return this.favoriteService.create({
|
||||
data: {
|
||||
company: {
|
||||
connect: { id: args.companyId },
|
||||
},
|
||||
workspaceId: workspace.id,
|
||||
},
|
||||
select: prismaSelect.value,
|
||||
});
|
||||
}
|
||||
|
||||
@Mutation(() => Favorite, {
|
||||
nullable: false,
|
||||
})
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(DeleteFavoriteAbilityHandler)
|
||||
async deleteFavorite(
|
||||
@Args('where') args: FavoriteWhereInput,
|
||||
@AuthWorkspace() workspace: Workspace,
|
||||
@PrismaSelector({ modelName: 'Favorite' })
|
||||
prismaSelect: PrismaSelect<'Favorite'>,
|
||||
): Promise<Partial<Favorite>> {
|
||||
const favorite = await this.favoriteService.findFirst({
|
||||
where: { ...args, workspaceId: workspace.id },
|
||||
});
|
||||
|
||||
return this.favoriteService.delete({
|
||||
where: { id: favorite?.id },
|
||||
select: prismaSelect.value,
|
||||
});
|
||||
}
|
||||
}
|
||||
39
server/src/core/favorite/services/favorite.service.ts
Normal file
39
server/src/core/favorite/services/favorite.service.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { PrismaService } from 'src/database/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class FavoriteService {
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
|
||||
// Find
|
||||
findFirst = this.prismaService.client.favorite.findFirst;
|
||||
findFirstOrThrow = this.prismaService.client.favorite.findFirstOrThrow;
|
||||
|
||||
findUnique = this.prismaService.client.favorite.findUnique;
|
||||
findUniqueOrThrow = this.prismaService.client.favorite.findUniqueOrThrow;
|
||||
|
||||
findMany = this.prismaService.client.favorite.findMany;
|
||||
|
||||
// Create
|
||||
create = this.prismaService.client.favorite.create;
|
||||
createMany = this.prismaService.client.favorite.createMany;
|
||||
|
||||
// Update
|
||||
update = this.prismaService.client.favorite.update;
|
||||
upsert = this.prismaService.client.favorite.upsert;
|
||||
updateMany = this.prismaService.client.favorite.updateMany;
|
||||
|
||||
// Delete
|
||||
delete = this.prismaService.client.favorite.delete;
|
||||
deleteMany = this.prismaService.client.favorite.deleteMany;
|
||||
|
||||
// Aggregate
|
||||
aggregate = this.prismaService.client.favorite.aggregate;
|
||||
|
||||
// Count
|
||||
count = this.prismaService.client.favorite.count;
|
||||
|
||||
// GroupBy
|
||||
groupBy = this.prismaService.client.favorite.groupBy;
|
||||
}
|
||||
@ -205,8 +205,9 @@ model WorkspaceMember {
|
||||
/// @TypeGraphQL.omit(input: true, output: true)
|
||||
deletedAt DateTime?
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
Favorite Favorite[]
|
||||
|
||||
@@map("workspace_members")
|
||||
}
|
||||
@ -246,6 +247,7 @@ model Company {
|
||||
updatedAt DateTime @updatedAt
|
||||
ActivityTarget ActivityTarget[]
|
||||
PipelineProgress PipelineProgress[]
|
||||
Favorite Favorite[]
|
||||
|
||||
@@map("companies")
|
||||
}
|
||||
@ -297,6 +299,7 @@ model Person {
|
||||
updatedAt DateTime @updatedAt
|
||||
ActivityTarget ActivityTarget[]
|
||||
PipelineProgress PipelineProgress[]
|
||||
Favorite Favorite[]
|
||||
|
||||
@@map("people")
|
||||
}
|
||||
@ -559,6 +562,22 @@ model Attachment {
|
||||
@@map("attachments")
|
||||
}
|
||||
|
||||
model Favorite {
|
||||
id String @id @default(uuid())
|
||||
workspaceId String?
|
||||
/// @TypeGraphQL.omit(input: true, output: false)
|
||||
person Person? @relation(fields: [personId], references: [id])
|
||||
personId String?
|
||||
/// @TypeGraphQL.omit(input: true, output: false)
|
||||
company Company? @relation(fields: [companyId], references: [id])
|
||||
companyId String?
|
||||
/// @TypeGraphQL.omit(input: true, output: false)
|
||||
workspaceMember WorkspaceMember? @relation(fields: [workspaceMemberId], references: [id])
|
||||
workspaceMemberId String?
|
||||
|
||||
@@map("favorites")
|
||||
}
|
||||
|
||||
enum ViewType {
|
||||
Table
|
||||
Pipeline
|
||||
|
||||
@ -16,6 +16,7 @@ export type ModelSelectMap = {
|
||||
PipelineStage: Prisma.PipelineStageSelect;
|
||||
PipelineProgress: Prisma.PipelineProgressSelect;
|
||||
Attachment: Prisma.AttachmentSelect;
|
||||
Favorite: Prisma.FavoriteSelect;
|
||||
View: Prisma.ViewSelect;
|
||||
ViewSort: Prisma.ViewSortSelect;
|
||||
ViewField: Prisma.ViewFieldSelect;
|
||||
|
||||
Reference in New Issue
Block a user