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:
@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user