import { Resolver, Arg, Mutation, Ctx, Authorized, FieldResolver, Root } from 'type-graphql'; import AccountModel from '@src/accounts/account.model'; import { ApolloContext } from '@lib/seed/interfaces/context'; import { AddInput, FavoriteComponent, RemoveInput } from '@services/module-favorites/components'; import { ApolloError } from 'apollo-server-lambda'; import { modelsLoaders } from '@src/__indexes/__loaders'; import { BaseGraphModel } from '@lib/seed/graphql/BaseGraphModel'; import _ from 'lodash'; import { SuccessResponse } from '@lib/seed/interfaces/response'; import { oneToManyComplexity } from '@lib/seed/graphql/Middleware'; @Resolver(AccountModel) export class AccountFavResolver { /* ███████╗██╗███████╗██╗ ██████╗ ███████╗ ██╔════╝██║██╔════╝██║ ██╔══██╗██╔════╝ █████╗ ██║█████╗ ██║ ██║ ██║███████╗ ██╔══╝ ██║██╔══╝ ██║ ██║ ██║╚════██║ ██║ ██║███████╗███████╗██████╔╝███████║ ╚═╝ ╚═╝╚══════╝╚══════╝╚═════╝ ╚══════╝ */ @FieldResolver(() => [FavoriteComponent], { complexity: oneToManyComplexity }) async getFavorites(@Root() account: AccountModel, @Ctx() ctx: ApolloContext): Promise<(FavoriteComponent | Error)[]> { const favLikes = account.favLikes || []; return _.filter(favLikes, { favorite: true }); } /* ██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗ ██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝ ██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝ ██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝ ╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║ ╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ */ /* ███╗ ███╗██╗ ██╗████████╗ █████╗ ████████╗ ██████╗ ██████╗ ███████╗ ████╗ ████║██║ ██║╚══██╔══╝██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝ ██╔████╔██║██║ ██║ ██║ ███████║ ██║ ██║ ██║██████╔╝███████╗ ██║╚██╔╝██║██║ ██║ ██║ ██╔══██║ ██║ ██║ ██║██╔══██╗╚════██║ ██║ ╚═╝ ██║╚██████╔╝ ██║ ██║ ██║ ██║ ╚██████╔╝██║ ██║███████║ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ */ @Mutation(() => FavoriteComponent) @Authorized() async accountsFavoritesAddOne(@Arg('input') input: AddInput, @Ctx() ctx: ApolloContext): Promise { const currentAccount = ctx.ctx.user; // Check if element exists if (!modelsLoaders[input.ressourceType]) throw new ApolloError('badConfiguration', '400'); const ressourceModel = modelsLoaders[input.ressourceType]; await ressourceModel.getOne({ _id: input.ressourceId }, ctx); const elementToSave: FavoriteComponent = { ...input, favorite: true, addedAt: new Date(), }; const favLikes: FavoriteComponent[] = currentAccount.favLikes || []; // Check if already in the sub array const index = _.findIndex(favLikes, { ressourceId: input.ressourceId }); if (index === -1) favLikes.push(elementToSave); else { favLikes[index].favorite = true; } await currentAccount.updateOne({ _id: currentAccount._id }, { favLikes: favLikes }, ctx); return elementToSave; } @Mutation(() => SuccessResponse) @Authorized() async accountsFavoritesRemoveOne(@Arg('input') input: RemoveInput, @Ctx() ctx: ApolloContext): Promise { const currentAccount = ctx.ctx.user; const favLikes: FavoriteComponent[] = currentAccount.favLikes || []; // Check if already in the sub array const index = _.findIndex(favLikes, { ressourceId: input.ressourceId }); if (index === -1) return { success: true }; else { // Check needs to remove entirely or just from the field if (favLikes[index].liked === undefined) favLikes.splice(index, 1); else favLikes[index].favorite = undefined; } await currentAccount.updateOne({ _id: currentAccount._id }, { favLikes: favLikes }, ctx); return { success: true }; } } @Resolver(AccountModel) export class AccountLikeResolver { /* ███████╗██╗███████╗██╗ ██████╗ ███████╗ ██╔════╝██║██╔════╝██║ ██╔══██╗██╔════╝ █████╗ ██║█████╗ ██║ ██║ ██║███████╗ ██╔══╝ ██║██╔══╝ ██║ ██║ ██║╚════██║ ██║ ██║███████╗███████╗██████╔╝███████║ ╚═╝ ╚═╝╚══════╝╚══════╝╚═════╝ ╚══════╝ */ @FieldResolver(() => [FavoriteComponent], { complexity: oneToManyComplexity }) async getLiked(@Root() account: AccountModel, @Ctx() ctx: ApolloContext): Promise<(FavoriteComponent | Error)[]> { const favLikes = account.favLikes || []; return _.filter(favLikes, { liked: true }); } /* ██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗ ██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝ ██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝ ██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝ ╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║ ╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ */ /* ███╗ ███╗██╗ ██╗████████╗ █████╗ ████████╗ ██████╗ ██████╗ ███████╗ ████╗ ████║██║ ██║╚══██╔══╝██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝ ██╔████╔██║██║ ██║ ██║ ███████║ ██║ ██║ ██║██████╔╝███████╗ ██║╚██╔╝██║██║ ██║ ██║ ██╔══██║ ██║ ██║ ██║██╔══██╗╚════██║ ██║ ╚═╝ ██║╚██████╔╝ ██║ ██║ ██║ ██║ ╚██████╔╝██║ ██║███████║ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ */ @Mutation(() => FavoriteComponent) @Authorized() async accountsLikesAddOne(@Arg('input') input: AddInput, @Ctx() ctx: ApolloContext): Promise { const currentAccount = ctx.ctx.user; // Check if element exists if (!modelsLoaders[input.ressourceType]) throw new ApolloError('badConfiguration', '400'); const ressourceModel = modelsLoaders[input.ressourceType]; await ressourceModel.getOne({ _id: input.ressourceId }, ctx); const elementToSave: FavoriteComponent = { ...input, liked: true, addedAt: new Date(), }; const favLikes: FavoriteComponent[] = currentAccount.favLikes || []; // Check if already in the sub array const index = _.findIndex(favLikes, { ressourceId: input.ressourceId }); if (index === -1) favLikes.push(elementToSave); else { favLikes[index].liked = true; } await currentAccount.updateOne({ _id: currentAccount._id }, { favLikes: favLikes }, ctx); return elementToSave; } @Mutation(() => FavoriteComponent) @Authorized() async accountsDislikesAddOne(@Arg('input') input: AddInput, @Ctx() ctx: ApolloContext): Promise { const currentAccount = ctx.ctx.user; // Check if element exists if (!modelsLoaders[input.ressourceType]) throw new ApolloError('badConfiguration', '400'); const ressourceModel = modelsLoaders[input.ressourceType]; await ressourceModel.getOne({ _id: input.ressourceId }, ctx); const elementToSave: FavoriteComponent = { ...input, liked: false, addedAt: new Date(), }; const favLikes: FavoriteComponent[] = currentAccount.favLikes || []; // Check if already in the sub array const index = _.findIndex(favLikes, { ressourceId: input.ressourceId }); if (index === -1) favLikes.push(elementToSave); else { favLikes[index].liked = false; } await currentAccount.updateOne({ _id: currentAccount._id }, { favLikes: favLikes }, ctx); return elementToSave; } @Mutation(() => SuccessResponse) @Authorized() async accountsLikesRemoveOne(@Arg('input') input: RemoveInput, @Ctx() ctx: ApolloContext): Promise { const currentAccount = ctx.ctx.user; const favLikes: FavoriteComponent[] = currentAccount.favLikes || []; // Check if already in the sub array const index = _.findIndex(favLikes, { ressourceId: input.ressourceId }); if (index === -1) return { success: true }; else { // Check needs to remove entirely or just from the field if (favLikes[index].favorite === undefined) favLikes.splice(index, 1); else favLikes[index].liked = undefined; } await currentAccount.updateOne({ _id: currentAccount._id }, { favLikes: favLikes }, ctx); return { success: true }; } }