feat: onboarding & profile edition (#507)
* feat: wip onboarding * fix: generate graphql front * wip: onboarding * feat: login/register and edit profile * fix: unused import * fix: test * Use DEBUG_MODE instead of STAGE and mute typescript depth exceed errors * Fix seeds * Fix onboarding when coming from google * Fix * Fix lint * Fix ci * Fix tests --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -1,10 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UserService } from './user.service';
|
||||
import { UserResolver } from './user.resolver';
|
||||
import { WorkspaceModule } from '../workspace/workspace.module';
|
||||
|
||||
@Module({
|
||||
imports: [WorkspaceModule],
|
||||
imports: [],
|
||||
providers: [UserService, UserResolver],
|
||||
exports: [UserService],
|
||||
})
|
||||
|
||||
@ -1,4 +1,11 @@
|
||||
import { Args, Resolver, Query, ResolveField, Parent } from '@nestjs/graphql';
|
||||
import {
|
||||
Args,
|
||||
Resolver,
|
||||
Query,
|
||||
ResolveField,
|
||||
Parent,
|
||||
Mutation,
|
||||
} from '@nestjs/graphql';
|
||||
import { UserService } from './user.service';
|
||||
import { FindManyUserArgs } from 'src/core/@generated/user/find-many-user.args';
|
||||
import { User } from 'src/core/@generated/user/user.model';
|
||||
@ -11,16 +18,40 @@ import {
|
||||
} from 'src/decorators/prisma-select.decorator';
|
||||
import { AbilityGuard } from 'src/guards/ability.guard';
|
||||
import { CheckAbilities } from 'src/decorators/check-abilities.decorator';
|
||||
import { ReadUserAbilityHandler } from 'src/ability/handlers/user.ability-handler';
|
||||
import {
|
||||
ReadUserAbilityHandler,
|
||||
UpdateUserAbilityHandler,
|
||||
} from 'src/ability/handlers/user.ability-handler';
|
||||
import { UserAbility } from 'src/decorators/user-ability.decorator';
|
||||
import { AppAbility } from 'src/ability/ability.factory';
|
||||
import { accessibleBy } from '@casl/prisma';
|
||||
import { AuthUser } from 'src/decorators/auth-user.decorator';
|
||||
import { assert } from 'src/utils/assert';
|
||||
import { UpdateOneUserArgs } from '../@generated/user/update-one-user.args';
|
||||
import { Prisma } from '@prisma/client';
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Resolver(() => User)
|
||||
export class UserResolver {
|
||||
constructor(private readonly userService: UserService) {}
|
||||
|
||||
@Query(() => User)
|
||||
async currentUser(
|
||||
@AuthUser() { id }: User,
|
||||
@PrismaSelector({ modelName: 'User' })
|
||||
prismaSelect: PrismaSelect<'User'>,
|
||||
) {
|
||||
const user = await this.userService.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
select: prismaSelect.value,
|
||||
});
|
||||
assert(user, 'User not found');
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@UseFilters(ExceptionFilter)
|
||||
@Query(() => [User], {
|
||||
nullable: false,
|
||||
@ -34,16 +65,44 @@ export class UserResolver {
|
||||
prismaSelect: PrismaSelect<'User'>,
|
||||
): Promise<Partial<User>[]> {
|
||||
return await this.userService.findMany({
|
||||
...args,
|
||||
where: args.where
|
||||
? {
|
||||
AND: [args.where, accessibleBy(ability).User],
|
||||
}
|
||||
: accessibleBy(ability).User,
|
||||
orderBy: args.orderBy,
|
||||
cursor: args.cursor,
|
||||
take: args.take,
|
||||
skip: args.skip,
|
||||
distinct: args.distinct,
|
||||
select: prismaSelect.value,
|
||||
});
|
||||
}
|
||||
|
||||
@Mutation(() => User)
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(UpdateUserAbilityHandler)
|
||||
async updateUser(
|
||||
@Args() args: UpdateOneUserArgs,
|
||||
@AuthUser() { id }: User,
|
||||
@PrismaSelector({ modelName: 'User' })
|
||||
prismaSelect: PrismaSelect<'User'>,
|
||||
) {
|
||||
const user = await this.userService.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
select: prismaSelect.value,
|
||||
});
|
||||
assert(user, 'User not found');
|
||||
|
||||
return this.userService.update({
|
||||
where: args.where,
|
||||
data: args.data,
|
||||
select: prismaSelect.value,
|
||||
} as Prisma.UserUpdateArgs);
|
||||
}
|
||||
|
||||
@ResolveField(() => String, {
|
||||
nullable: false,
|
||||
})
|
||||
|
||||
@ -2,8 +2,6 @@ import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { UserService } from './user.service';
|
||||
import { PrismaService } from 'src/database/prisma.service';
|
||||
import { prismaMock } from 'src/database/client-mock/jest-prisma-singleton';
|
||||
import { WorkspaceService } from '../workspace/services/workspace.service';
|
||||
import { WorkspaceMemberService } from '../workspace/services/workspace-member.service';
|
||||
|
||||
describe('UserService', () => {
|
||||
let service: UserService;
|
||||
@ -16,14 +14,6 @@ describe('UserService', () => {
|
||||
provide: PrismaService,
|
||||
useValue: prismaMock,
|
||||
},
|
||||
{
|
||||
provide: WorkspaceService,
|
||||
useValue: {},
|
||||
},
|
||||
{
|
||||
provide: WorkspaceMemberService,
|
||||
useValue: {},
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
|
||||
@ -1,10 +1,5 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
} from '@nestjs/common';
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from 'src/database/prisma.service';
|
||||
import { WorkspaceService } from 'src/core/workspace/services/workspace.service';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { assert } from 'src/utils/assert';
|
||||
|
||||
@ -15,10 +10,7 @@ export type UserPayload = {
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(
|
||||
private readonly prismaService: PrismaService,
|
||||
private readonly workspaceService: WorkspaceService,
|
||||
) {}
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
|
||||
// Find
|
||||
findFirst = this.prismaService.user.findFirst;
|
||||
@ -56,22 +48,6 @@ export class UserService {
|
||||
args: Prisma.SelectSubset<T, Prisma.UserCreateArgs>,
|
||||
): Promise<Prisma.UserGetPayload<T>> {
|
||||
assert(args.data.email, 'email is missing', BadRequestException);
|
||||
assert(args.data.firstName, 'firstName is missing', BadRequestException);
|
||||
assert(args.data.lastName, 'lastName is missing', BadRequestException);
|
||||
|
||||
const emailDomain = args.data.email.split('@')[1];
|
||||
|
||||
assert(emailDomain, 'Email is malformed', BadRequestException);
|
||||
|
||||
const workspace = await this.workspaceService.findUnique({
|
||||
where: { domainName: emailDomain },
|
||||
});
|
||||
|
||||
assert(
|
||||
workspace,
|
||||
'User email domain does not match an existing workspace',
|
||||
ForbiddenException,
|
||||
);
|
||||
|
||||
const user = await this.prismaService.user.upsert({
|
||||
where: {
|
||||
@ -79,10 +55,12 @@ export class UserService {
|
||||
},
|
||||
create: {
|
||||
...(args.data as Prisma.UserCreateInput),
|
||||
// Assign the user to a new workspace by default
|
||||
workspaceMember: {
|
||||
connectOrCreate: {
|
||||
where: { id: workspace.id },
|
||||
create: { workspaceId: workspace.id },
|
||||
create: {
|
||||
workspace: {
|
||||
create: {},
|
||||
},
|
||||
},
|
||||
},
|
||||
locale: 'en',
|
||||
@ -90,7 +68,7 @@ export class UserService {
|
||||
update: {},
|
||||
...(args.select ? { select: args.select } : {}),
|
||||
...(args.include ? { include: args.include } : {}),
|
||||
});
|
||||
} as Prisma.UserUpsertArgs);
|
||||
|
||||
return user as Prisma.UserGetPayload<T>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user