@@ -19,68 +19,59 @@ class MemberOrganizationsRepository {
1919 memberId : string ,
2020 options : IRepositoryOptions ,
2121 ) : Promise < IRenderFriendlyMemberOrganization [ ] > {
22- const transaction = await SequelizeRepository . createTransaction ( options )
23- try {
24- const txOptions = { ...options , transaction }
25- const qx = SequelizeRepository . getQueryExecutor ( txOptions , transaction )
26-
27- // Fetch member organizations
28- const memberOrganizations : IMemberOrganization [ ] = await fetchMemberOrganizations (
29- qx ,
30- memberId ,
31- )
32-
33- // Parse unique organization ids
34- const orgIds : string [ ] = [ ...new Set ( memberOrganizations . map ( ( mo ) => mo . organizationId ) ) ]
35-
36- // Fetch organizations
37- let organizations : IOrganizationSummary [ ] = [ ]
38- if ( orgIds . length ) {
39- organizations = await queryOrgs ( qx , {
40- filter : {
41- [ OrganizationField . ID ] : {
42- in : orgIds ,
43- } ,
44- } ,
45- fields : [ OrganizationField . ID , OrganizationField . DISPLAY_NAME , OrganizationField . LOGO ] ,
46- } )
47- }
48-
49- // Fetch affiliation overrides
50- const affiliationOverrides = await findMemberOrganizationAffiliationOverrides (
51- qx ,
52- memberId ,
53- memberOrganizations . map ( ( mo ) => mo . id ) ,
54- )
55-
56- // Create mapping by id to speed up the processing
57- const orgByid : Record < string , IOrganizationSummary > = organizations . reduce (
58- ( obj : Record < string , IOrganizationSummary > , org ) => ( {
59- ...obj ,
60- [ org . id ] : org ,
61- } ) ,
62- { } ,
63- )
64-
65- // Format the results
66- const result : IRenderFriendlyMemberOrganization [ ] = memberOrganizations . map ( ( mo ) => ( {
67- ...( orgByid [ mo . organizationId ] || { } ) ,
68- id : mo . organizationId ,
69- memberOrganizations : {
70- ...mo ,
71- affiliationOverride : affiliationOverrides . find ( ( ao ) => ao . memberOrganizationId === mo . id ) ,
72- } ,
73- } ) )
22+ const qx = SequelizeRepository . getQueryExecutor ( options )
7423
75- await SequelizeRepository . commitTransaction ( transaction )
24+ // Fetch member organizations
25+ const memberOrganizations : IMemberOrganization [ ] = await fetchMemberOrganizations (
26+ qx ,
27+ memberId ,
28+ )
7629
77- return result
78- } catch ( err ) {
79- if ( transaction ) {
80- await SequelizeRepository . rollbackTransaction ( transaction )
81- }
82- throw err
30+ if ( memberOrganizations . length === 0 ) {
31+ return [ ]
8332 }
33+
34+ // Parse unique organization ids
35+ const orgIds : string [ ] = [ ...new Set ( memberOrganizations . map ( ( mo ) => mo . organizationId ) ) ]
36+
37+ // Fetch organizations
38+ let organizations : IOrganizationSummary [ ] = [ ]
39+ if ( orgIds . length ) {
40+ organizations = await queryOrgs ( qx , {
41+ filter : {
42+ [ OrganizationField . ID ] : {
43+ in : orgIds ,
44+ } ,
45+ } ,
46+ fields : [ OrganizationField . ID , OrganizationField . DISPLAY_NAME , OrganizationField . LOGO ] ,
47+ } )
48+ }
49+
50+ // Fetch affiliation overrides
51+ const affiliationOverrides = await findMemberOrganizationAffiliationOverrides (
52+ qx ,
53+ memberId ,
54+ memberOrganizations . map ( ( mo ) => mo . id ) ,
55+ )
56+
57+ // Create mapping by id to speed up the processing
58+ const orgByid : Record < string , IOrganizationSummary > = organizations . reduce (
59+ ( obj : Record < string , IOrganizationSummary > , org ) => ( {
60+ ...obj ,
61+ [ org . id ] : org ,
62+ } ) ,
63+ { } ,
64+ )
65+
66+ // Format the results
67+ return memberOrganizations . map ( ( mo ) => ( {
68+ ...( orgByid [ mo . organizationId ] || { } ) ,
69+ id : mo . organizationId ,
70+ memberOrganizations : {
71+ ...mo ,
72+ affiliationOverride : affiliationOverrides . find ( ( ao ) => ao . memberOrganizationId === mo . id ) ,
73+ } ,
74+ } ) )
8475 }
8576
8677 static async create (
0 commit comments