Skip to content

Commit 5ab680d

Browse files
committed
chore: send out rabbitmq message for visitors approval and cancellation
1 parent 3fee63e commit 5ab680d

5 files changed

Lines changed: 65 additions & 1 deletion

File tree

apps/backend/src/datasources/UserDataSource.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,11 @@ export interface UserDataSource {
9999
): Promise<boolean>;
100100
getRoleByShortCode(roleShortCode: Roles): Promise<Role>;
101101
mergeUsers(fromUserId: number, intoUserId: number): Promise<void>;
102+
getApprovedProposalVisitorsWithInstitution(proposalPk: number): Promise<
103+
{
104+
user: User;
105+
institution: Institution;
106+
country: Country;
107+
}[]
108+
>;
102109
}

apps/backend/src/datasources/mockups/UserDataSource.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,4 +559,14 @@ export class UserDataSourceMock implements UserDataSource {
559559
},
560560
];
561561
}
562+
563+
async getApprovedProposalVisitorsWithInstitution(proposalPk: number): Promise<
564+
{
565+
user: User;
566+
institution: Institution;
567+
country: Country;
568+
}[]
569+
> {
570+
return [];
571+
}
562572
}

apps/backend/src/datasources/postgres/UserDataSource.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,4 +831,29 @@ export default class PostgresUserDataSource implements UserDataSource {
831831
});
832832
});
833833
}
834+
835+
async getApprovedProposalVisitorsWithInstitution(proposalPk: number): Promise<
836+
{
837+
user: User;
838+
institution: Institution;
839+
country: Country;
840+
}[]
841+
> {
842+
return database
843+
.select('i.*', 'c.*', 'u.*')
844+
.from('users as u')
845+
.join('visits_has_users as vu', { 'u.user_id': 'vu.user_id' })
846+
.join('visits as v', { 'v.visit_id': 'vu.visit_id' })
847+
.leftJoin('institutions as i', { 'u.institution_id': 'i.institution_id' })
848+
.leftJoin('countries as c', { 'c.country_id': 'i.country_id' })
849+
.where('v.proposal_pk', proposalPk)
850+
.andWhere('vu.status', 'APPROVED')
851+
.then((users: (UserRecord & InstitutionRecord & CountryRecord)[]) => {
852+
return users.map((user) => ({
853+
user: createUserObject(user),
854+
institution: createInstitutionObject(user),
855+
country: createCountryObject(user),
856+
}));
857+
});
858+
}
834859
}

apps/backend/src/datasources/stfc/StfcUserDataSource.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,4 +774,10 @@ export class StfcUserDataSource implements UserDataSource {
774774
this.roleAssignmentMap.get(roleId) ?? ''
775775
);
776776
}
777+
778+
async getApprovedProposalVisitorsWithInstitution(
779+
proposalPk: number
780+
): Promise<{ user: User; institution: Institution; country: Country }[]> {
781+
throw new Error('Method not implemented.');
782+
}
777783
}

apps/backend/src/eventHandlers/messageBroker.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type ProposalMessageData = {
6161
instruments?: { id: number; shortCode: string; allocatedTime: number }[];
6262
members: Member[];
6363
dataAccessUsers: Member[];
64+
visitors: Member[];
6465
newStatus?: string;
6566
proposalPk: number;
6667
proposer?: Member;
@@ -129,10 +130,17 @@ export const getProposalMessageData = async (proposal: Proposal) => {
129130

130131
const proposalUsersWithInstitution =
131132
await userDataSource.getProposalUsersWithInstitution(proposal.primaryKey);
133+
132134
const dataAccessUsersWithInstitution =
133135
await dataAccessUsersDataSource.getDataAccessUsersWithInstitution(
134136
proposal.primaryKey
135137
);
138+
139+
const visitorsWithInstitution =
140+
await userDataSource.getApprovedProposalVisitorsWithInstitution(
141+
proposal.primaryKey
142+
);
143+
136144
const maybeInstruments =
137145
await instrumentDataSource.getInstrumentsByProposalPk(proposal.primaryKey);
138146

@@ -177,9 +185,11 @@ export const getProposalMessageData = async (proposal: Proposal) => {
177185
dataAccessUsers: dataAccessUsersWithInstitution.map(
178186
mapUserWithInstitutionToMember
179187
),
188+
visitors: visitorsWithInstitution.map(mapUserWithInstitutionToMember),
180189
newStatus: proposalStatus?.shortCode,
181190
submitted: proposal.submitted,
182191
};
192+
183193
const proposerWithInstitution = await userDataSource.getUserWithInstitution(
184194
proposal.proposerId
185195
);
@@ -242,7 +252,6 @@ export async function createPostToRabbitMQHandler() {
242252
case Event.PROPOSAL_DELETED:
243253
case Event.PROPOSAL_STATUS_ACTION_EXECUTED: {
244254
const jsonMessage = await getProposalMessageData(event.proposal);
245-
246255
await rabbitMQ.sendMessageToExchange(
247256
event.exchange || EXCHANGE_NAME,
248257
event.type,
@@ -380,6 +389,13 @@ export async function createPostToRabbitMQHandler() {
380389
: RABBITMQ_VISIT_EVENT_TYPE.VISIT_DELETED,
381390
jsonMessage
382391
);
392+
393+
const proposalJsonMessage = await getProposalMessageData(proposal!);
394+
await rabbitMQ.sendMessageToExchange(
395+
EXCHANGE_NAME,
396+
Event.PROPOSAL_UPDATED,
397+
proposalJsonMessage
398+
);
383399
break;
384400
}
385401
case Event.DATA_ACCESS_USERS_UPDATED: {

0 commit comments

Comments
 (0)