@@ -19,68 +19,56 @@ 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 ( qx , memberId )
7626
77- return result
78- } catch ( err ) {
79- if ( transaction ) {
80- await SequelizeRepository . rollbackTransaction ( transaction )
81- }
82- throw err
27+ if ( memberOrganizations . length === 0 ) {
28+ return [ ]
8329 }
30+
31+ // Parse unique organization ids
32+ const orgIds : string [ ] = [ ...new Set ( memberOrganizations . map ( ( mo ) => mo . organizationId ) ) ]
33+
34+ // Fetch organizations
35+ let organizations : IOrganizationSummary [ ] = [ ]
36+ if ( orgIds . length ) {
37+ organizations = await queryOrgs ( qx , {
38+ filter : {
39+ [ OrganizationField . ID ] : {
40+ in : orgIds ,
41+ } ,
42+ } ,
43+ fields : [ OrganizationField . ID , OrganizationField . DISPLAY_NAME , OrganizationField . LOGO ] ,
44+ } )
45+ }
46+
47+ // Fetch affiliation overrides
48+ const affiliationOverrides = await findMemberOrganizationAffiliationOverrides (
49+ qx ,
50+ memberId ,
51+ memberOrganizations . map ( ( mo ) => mo . id ) ,
52+ )
53+
54+ // Create mapping by id to speed up the processing
55+ const orgByid : Record < string , IOrganizationSummary > = organizations . reduce (
56+ ( obj : Record < string , IOrganizationSummary > , org ) => ( {
57+ ...obj ,
58+ [ org . id ] : org ,
59+ } ) ,
60+ { } ,
61+ )
62+
63+ // Format the results
64+ return memberOrganizations . map ( ( mo ) => ( {
65+ ...( orgByid [ mo . organizationId ] || { } ) ,
66+ id : mo . organizationId ,
67+ memberOrganizations : {
68+ ...mo ,
69+ affiliationOverride : affiliationOverrides . find ( ( ao ) => ao . memberOrganizationId === mo . id ) ,
70+ } ,
71+ } ) )
8472 }
8573
8674 static async create (
0 commit comments