Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/components/user/dto/assign-organization-to-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Field, InputType, ObjectType } from '@nestjs/graphql';
import { Type } from 'class-transformer';
import { ValidateNested } from 'class-validator';
import { type ID, IdField, MutationPlaceholderOutput } from '~/common';
import { type ID, IdField } from '~/common';
import { Partner } from '../../../components/partner/dto';

@InputType()
export class AssignOrganizationToUser {
Expand All @@ -20,8 +21,11 @@ export abstract class AssignOrganizationToUserInput {
@Field()
@Type(() => AssignOrganizationToUser)
@ValidateNested()
readonly request: AssignOrganizationToUser;
readonly assignment: AssignOrganizationToUser;
}

@ObjectType()
export abstract class AssignOrganizationToUserOutput extends MutationPlaceholderOutput {}
export abstract class AssignOrganizationToUserOutput {
@Field()
readonly partner: Partner;
}
10 changes: 7 additions & 3 deletions src/components/user/dto/remove-organization-from-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Field, InputType, ObjectType } from '@nestjs/graphql';
import { Type } from 'class-transformer';
import { ValidateNested } from 'class-validator';
import { type ID, IdField, MutationPlaceholderOutput } from '~/common';
import { type ID, IdField } from '~/common';
import { Partner } from '../../../components/partner/dto';

@InputType()
export class RemoveOrganizationFromUser {
Expand All @@ -17,8 +18,11 @@ export abstract class RemoveOrganizationFromUserInput {
@Field()
@Type(() => RemoveOrganizationFromUser)
@ValidateNested()
readonly request: RemoveOrganizationFromUser;
readonly assignment: RemoveOrganizationFromUser;
}

@ObjectType()
export abstract class RemoveOrganizationFromUserOutput extends MutationPlaceholderOutput {}
export abstract class RemoveOrganizationFromUserOutput {
@Field()
readonly partner: Partner;
}
4 changes: 4 additions & 0 deletions src/components/user/user.gel.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,8 @@ export class UserGelRepository
hydrateAsNeo4j(): never {
throw new NotImplementedException();
}

getPrimaryOrganizationId(userId: ID): Promise<ID | null> {
throw new NotImplementedException().with({ userId });
}
}
18 changes: 16 additions & 2 deletions src/components/user/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,13 @@ export class UserRepository extends DtoRepository(User) {
if (primary) {
q.subQuery((sub) =>
sub
.with('user, org')
.with('user')
.match([
node('user'),
relation('out', 'oldRel', 'primaryOrganization', {
active: true,
}),
node('org'),
node('anyOrg', 'Organization'),
])
.setValues({ 'oldRel.active': false })
.return('oldRel as oldPrimaryRel')
Expand Down Expand Up @@ -333,6 +333,20 @@ export class UserRepository extends DtoRepository(User) {
}
}

async getPrimaryOrganizationId(userId: ID): Promise<ID | null> {
const result = await this.db
.query()
.match([
node('user', 'User', { id: userId }),
relation('out', '', 'primaryOrganization', ACTIVE),
node('org', 'Organization'),
])
.return<{ orgId: ID }>('org.id as orgId')
.first();

return result?.orgId ?? null;
}

async removeOrganizationFromUser(request: RemoveOrganizationFromUser) {
const result = await this.db
.query()
Expand Down
28 changes: 22 additions & 6 deletions src/components/user/user.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import {
OrganizationListInput,
SecuredOrganizationList,
} from '../organization/dto';
import { PartnerLoader } from '../partner';
import { PartnerListInput, SecuredPartnerList } from '../partner/dto';
import { PartnerLoader, PartnerService } from '../partner';
import { Partner, PartnerListInput, SecuredPartnerList } from '../partner/dto';
import { TimeZoneService } from '../timezone';
import { SecuredTimeZone } from '../timezone/timezone.dto';
import {
Expand Down Expand Up @@ -70,6 +70,7 @@ class ModifyLocationArgs {
export class UserResolver {
constructor(
private readonly userService: UserService,
private readonly partnerService: PartnerService,
private readonly timeZoneService: TimeZoneService,
private readonly identity: Identity,
) {}
Expand Down Expand Up @@ -164,6 +165,14 @@ export class UserResolver {
return list;
}

@ResolveField(() => Partner, { nullable: true })
async primaryOrganization(@Parent() { id }: User): Promise<Partner | null> {
const primaryOrgId = await this.userService.getPrimaryOrganizationId(id);
return primaryOrgId
? await this.partnerService.readOnePartnerByOrgId(primaryOrgId)
: null;
}

@ResolveField(() => SecuredPartnerList)
async partners(
@Parent() { id }: User,
Expand Down Expand Up @@ -263,8 +272,12 @@ export class UserResolver {
async assignOrganizationToUser(
@Args('input') input: AssignOrganizationToUserInput,
): Promise<AssignOrganizationToUserOutput> {
await this.userService.assignOrganizationToUser(input.request);
return { success: true };
await this.userService.assignOrganizationToUser(input.assignment);
const partner = await this.partnerService.readOnePartnerByOrgId(
input.assignment.orgId,
);

return { partner };
}

@Mutation(() => RemoveOrganizationFromUserOutput, {
Expand All @@ -273,8 +286,11 @@ export class UserResolver {
async removeOrganizationFromUser(
@Args('input') input: RemoveOrganizationFromUserInput,
): Promise<RemoveOrganizationFromUserOutput> {
await this.userService.removeOrganizationFromUser(input.request);
return { success: true };
await this.userService.removeOrganizationFromUser(input.assignment);
const partner = await this.partnerService.readOnePartnerByOrgId(
input.assignment.orgId,
);
return { partner };
}

@Mutation(() => User, {
Expand Down
5 changes: 5 additions & 0 deletions src/components/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ export class UserService {
await this.userRepo.assignOrganizationToUser(request);
}

async getPrimaryOrganizationId(userId: ID) {
const org = await this.userRepo.getPrimaryOrganizationId(userId);
return org ?? null;
}

async removeOrganizationFromUser(
request: RemoveOrganizationFromUser,
): Promise<void> {
Expand Down
Loading