2021-10-04 18:12:27 +00:00
|
|
|
import escape from 'escape-html';
|
2021-06-04 00:58:40 +00:00
|
|
|
import withSession from '../../lib/session';
|
|
|
|
|
import connectDb from '../../utils/db.js';
|
|
|
|
|
import Guest from '../../models/Guest';
|
|
|
|
|
|
|
|
|
|
export default withSession(async (req, res) => {
|
|
|
|
|
const {
|
|
|
|
|
query: { id },
|
|
|
|
|
method,
|
|
|
|
|
session,
|
|
|
|
|
} = req;
|
|
|
|
|
|
2022-01-11 01:14:30 +00:00
|
|
|
const { user } = session;
|
2021-06-04 00:58:40 +00:00
|
|
|
|
|
|
|
|
if (!user?.isLoggedIn) {
|
|
|
|
|
res.status(401).end();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 18:11:00 +00:00
|
|
|
// TODO: REMOVE THIS WHEN TAKING YOUR SITE TO PRODUCTION
|
|
|
|
|
if (process.env.SITE_ENV === 'TEST_SITE') {
|
|
|
|
|
res.status(200).json({ status: 'SUCCESS', groupId: 'TESTID_12345' });
|
2021-06-04 20:43:37 +00:00
|
|
|
} else {
|
2022-01-28 04:54:29 +00:00
|
|
|
const knex = await connectDb();
|
2021-06-04 20:43:37 +00:00
|
|
|
const { firstName, lastName } = await req.body;
|
2021-06-04 00:58:40 +00:00
|
|
|
|
2021-06-04 20:43:37 +00:00
|
|
|
try {
|
2022-01-28 04:54:29 +00:00
|
|
|
const result = await knex('guests')
|
2022-02-02 06:11:07 +00:00
|
|
|
.where(
|
|
|
|
|
knex.raw(
|
|
|
|
|
'LOWER("first_name") = ?',
|
|
|
|
|
`${escape(firstName.trim()).toLowerCase()}`
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.andWhere(
|
|
|
|
|
knex.raw(
|
|
|
|
|
'LOWER("last_name") = ?',
|
|
|
|
|
`${escape(lastName.trim()).toLowerCase()}`
|
|
|
|
|
)
|
|
|
|
|
)
|
2022-01-28 04:54:29 +00:00
|
|
|
.select(
|
|
|
|
|
'first_name',
|
|
|
|
|
'last_name',
|
|
|
|
|
'role',
|
|
|
|
|
'rsvp_status',
|
|
|
|
|
'dietary_notes',
|
|
|
|
|
'song_requests',
|
|
|
|
|
'has_plus_one',
|
|
|
|
|
'plus_one',
|
|
|
|
|
'plus_one_first_name',
|
|
|
|
|
'plus_one_last_name',
|
|
|
|
|
'party_id',
|
|
|
|
|
'search_count'
|
|
|
|
|
)
|
|
|
|
|
.first();
|
|
|
|
|
if (result) {
|
|
|
|
|
res.status(200).json({ status: 'SUCCESS', groupId: result.party_id });
|
|
|
|
|
} else {
|
|
|
|
|
res.status(400).json({ status: 'FAILURE' });
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-04 20:43:37 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
const { response: fetchResponse } = error;
|
|
|
|
|
res.status(fetchResponse?.status || 500).json({ status: 'FAILURE' });
|
|
|
|
|
}
|
2021-06-04 00:58:40 +00:00
|
|
|
}
|
|
|
|
|
});
|