42 lines
1.7 KiB
TypeScript
42 lines
1.7 KiB
TypeScript
import * as dotenv from 'dotenv';
|
|
import {ReceivedInvite} from './ReceivedInvite.interface';
|
|
import {PartyPlanerDB} from '../PartyPlaner.db';
|
|
|
|
dotenv.config();
|
|
|
|
/**
|
|
* Returns all events the user is invited to
|
|
* @param useDev If the dev or prod database should be used
|
|
* @param userId The userId of the user to fetch the friends for
|
|
* @return ReceivedInvite[] A list of invites
|
|
*/
|
|
export const getInvitesData = async (useDev: boolean, userId: string): Promise<ReceivedInvite[]> => {
|
|
let conn = PartyPlanerDB.getConnection(useDev);
|
|
try {
|
|
let rows = await conn.query('SELECT i.invite_id, i.valid_until, i.already_used, i.invite_key, e.name as event_name, e.description as event_description, e.takes_place_date, e.registration_until_date, e.max_participants, e.creator_id, u.first_name, u.last_name FROM invitations i LEFT OUTER JOIN events e ON e.event_id = i.event_id LEFT OUTER JOIN users u ON u.user_id = e.creator_id WHERE i.user_id = ?', userId);
|
|
|
|
let invites: ReceivedInvite[] = [];
|
|
|
|
for (let row of rows) {
|
|
invites.push({
|
|
inviteId: row.invite_id,
|
|
validUntil: row.valid_until,
|
|
alreadyUsed: row.already_used,
|
|
inviteKey: row.invite_key,
|
|
eventName: row.event_name,
|
|
eventDescription: row.event_description,
|
|
takesPlaceDate: row.takes_place_date,
|
|
registrationUntilDate: row.registration_until_date,
|
|
maxParticipants: row.max_participants,
|
|
eventCreatorId: row.creator_id,
|
|
creatorFirstName: row.first_name,
|
|
creatorLastName: row.last_name
|
|
});
|
|
}
|
|
|
|
return invites;
|
|
} catch (err) {
|
|
throw err;
|
|
}
|
|
};
|