Skip to content

Commit 6f22b46

Browse files
committed
fix primary organization to be overwritten and exposed to the API
1 parent a298e05 commit 6f22b46

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
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: 11 additions & 2 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 {
@@ -68,6 +68,7 @@ class ModifyLocationArgs {
6868
export class UserResolver {
6969
constructor(
7070
private readonly userService: UserService,
71+
private readonly partnerService: PartnerService,
7172
private readonly timeZoneService: TimeZoneService,
7273
private readonly identity: Identity,
7374
) {}
@@ -162,6 +163,14 @@ export class UserResolver {
162163
return list;
163164
}
164165

166+
@ResolveField(() => Partner, { nullable: true })
167+
async primaryOrganization(@Parent() { id }: User): Promise<Partner | null> {
168+
const primaryOrgId = await this.userService.getPrimaryOrganizationId(id);
169+
return primaryOrgId
170+
? await this.partnerService.readOnePartnerByOrgId(primaryOrgId)
171+
: null;
172+
}
173+
165174
@ResolveField(() => SecuredPartnerList)
166175
async partners(
167176
@Parent() { id }: 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)