Skip to content

Commit 0b6c28f

Browse files
committed
fix primary organization to be overwritten and exposed to the API
1 parent 2b50986 commit 0b6c28f

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

src/components/user/user.repository.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,13 @@ export class UserRepository extends DtoRepository(User) {
290290
if (primary) {
291291
q.subQuery((sub) =>
292292
sub
293-
.with('user, org')
293+
.with('user')
294294
.match([
295295
node('user'),
296296
relation('out', 'oldRel', 'primaryOrganization', {
297297
active: true,
298298
}),
299-
node('org'),
299+
node('anyOrg', 'Organization'),
300300
])
301301
.setValues({ 'oldRel.active': false })
302302
.return('oldRel as oldPrimaryRel')
@@ -333,6 +333,20 @@ export class UserRepository extends DtoRepository(User) {
333333
}
334334
}
335335

336+
async getPrimaryOrganizationId(userId: ID): Promise<ID | null> {
337+
const result = await this.db
338+
.query()
339+
.match([
340+
node('user', 'User', { id: userId }),
341+
relation('out', '', 'primaryOrganization', ACTIVE),
342+
node('org', 'Organization'),
343+
])
344+
.return<{ orgId: ID }>('org.id as orgId')
345+
.first();
346+
347+
return result?.orgId ?? null;
348+
}
349+
336350
async removeOrganizationFromUser(request: RemoveOrganizationFromUser) {
337351
const result = await this.db
338352
.query()

src/components/user/user.resolver.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import {
2525
OrganizationListInput,
2626
SecuredOrganizationList,
2727
} from '../organization/dto';
28-
import { PartnerLoader } from '../partner';
29-
import { PartnerListInput, SecuredPartnerList } from '../partner/dto';
28+
import { PartnerLoader, PartnerService } from '../partner';
29+
import { Partner, PartnerListInput, SecuredPartnerList } from '../partner/dto';
3030
import { TimeZoneService } from '../timezone';
3131
import { SecuredTimeZone } from '../timezone/timezone.dto';
3232
import {
@@ -70,6 +70,7 @@ class ModifyLocationArgs {
7070
export class UserResolver {
7171
constructor(
7272
private readonly userService: UserService,
73+
private readonly partnerService: PartnerService,
7374
private readonly timeZoneService: TimeZoneService,
7475
private readonly identity: Identity,
7576
) {}
@@ -164,6 +165,14 @@ export class UserResolver {
164165
return list;
165166
}
166167

168+
@ResolveField(() => Partner, { nullable: true })
169+
async primaryOrganization(@Parent() { id }: User): Promise<Partner | null> {
170+
const primaryOrgId = await this.userService.getPrimaryOrganizationId(id);
171+
return primaryOrgId
172+
? await this.partnerService.readOnePartnerByOrgId(primaryOrgId)
173+
: null;
174+
}
175+
167176
@ResolveField(() => SecuredPartnerList)
168177
async partners(
169178
@Parent() { id }: User,
@@ -263,8 +272,12 @@ export class UserResolver {
263272
async assignOrganizationToUser(
264273
@Args('input') input: AssignOrganizationToUserInput,
265274
): Promise<AssignOrganizationToUserOutput> {
266-
await this.userService.assignOrganizationToUser(input.request);
267-
return { success: true };
275+
await this.userService.assignOrganizationToUser(input.assignment);
276+
const partner = await this.partnerService.readOnePartnerByOrgId(
277+
input.assignment.orgId,
278+
);
279+
280+
return { partner };
268281
}
269282

270283
@Mutation(() => RemoveOrganizationFromUserOutput, {
@@ -273,8 +286,11 @@ export class UserResolver {
273286
async removeOrganizationFromUser(
274287
@Args('input') input: RemoveOrganizationFromUserInput,
275288
): Promise<RemoveOrganizationFromUserOutput> {
276-
await this.userService.removeOrganizationFromUser(input.request);
277-
return { success: true };
289+
await this.userService.removeOrganizationFromUser(input.assignment);
290+
const partner = await this.partnerService.readOnePartnerByOrgId(
291+
input.assignment.orgId,
292+
);
293+
return { partner };
278294
}
279295

280296
@Mutation(() => User, {

src/components/user/user.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ export class UserService {
333333
await this.userRepo.assignOrganizationToUser(request);
334334
}
335335

336+
async getPrimaryOrganizationId(userId: ID) {
337+
const org = await this.userRepo.getPrimaryOrganizationId(userId);
338+
return org ?? null;
339+
}
340+
336341
async removeOrganizationFromUser(
337342
request: RemoveOrganizationFromUser,
338343
): Promise<void> {

0 commit comments

Comments
 (0)