Skip to content

Commit

Permalink
added routes for testing APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
subru-37 committed Nov 3, 2024
1 parent a356e21 commit 99b8c35
Show file tree
Hide file tree
Showing 5 changed files with 6,249 additions and 2,251 deletions.
220 changes: 176 additions & 44 deletions apps/core-admin/src/controllers/mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,27 @@ export const sendMailWithQR = async (req: Request, res: Response) => {
}
}
}
res.status(200).json({
return res.status(200).json({
success: RecipientsMailed,
failure: RecipientsNotMailed,
nSuccess: RecipientsMailed.length,
nFailure: RecipientsNotMailed.length,
});
} else {
res.status(400).send({ message: 'Invalid Project ID / project ID not found' });
return res.status(400).send({ message: 'Invalid Project ID / project ID not found' });
}
} else {
res.status(400).send({ message: 'Incorrect project ID or invalid supabase credentials' });
return res
.status(400)
.send({ message: 'Incorrect project ID or invalid supabase credentials' });
}
} else {
res.status(500).send({ message: 'Invalid Supabase Credentials' });
return res.status(500).send({ message: 'Invalid Supabase Credentials' });
}
}
} catch (e: any) {
console.error(e);
res.status(400).send({ message: e });
return res.status(400).send({ message: e });
}
};

Expand All @@ -121,24 +123,24 @@ export const getMailStatus = async (req: Request, res: Response) => {
});
if (emailStatus && emailStatus.status == 200) {
console.log({ ...emailStatus.data.status });
res.send(200).json({
return res.send(200).json({
...emailStatus.data.status,
});
} else {
res.status(400).send({ message: 'JobId not found', error: emailStatus.data });
return res.status(400).send({ message: 'JobId not found', error: emailStatus.data });
}
} else {
res.status(400).send({ message: 'Email Job ID not found, send email again' });
return res.status(400).send({ message: 'Email Job ID not found, send email again' });
}
} else {
res.status(400).send({ message: 'Email Job not found, send email again' });
return res.status(400).send({ message: 'Email Job not found, send email again' });
}
} else {
res.status(500).send({ message: 'Invalid Supabase Credentials' });
return res.status(500).send({ message: 'Invalid Supabase Credentials' });
}
} catch (e: any) {
console.error(e);
res.status(400).send({ message: e });
return res.status(400).send({ message: e });
}
};

Expand All @@ -165,17 +167,19 @@ export const newMailProject = async (req: Request, res: Response) => {
}
} else {
const currentProject = await supabase.from('Projects').select('*').eq('name', name);
res.status(200).send({ message: 'Project Already Exists', ...currentProject.data });
return res
.status(200)
.send({ message: 'Project Already Exists', ...currentProject.data });
}
} else {
res.status(400).send({ message: 'Error', data: response.data });
return res.status(400).send({ message: 'Error', data: response.data });
}
} else {
res.status(500).send({ message: 'Invalid Supabase Credentials' });
return res.status(500).send({ message: 'Invalid Supabase Credentials' });
}
} catch (e: any) {
console.error(e);
res.status(400).send({ message: e });
return res.status(400).send({ message: e });
}
};

Expand All @@ -188,55 +192,183 @@ export const getMailProjects = async (req: Request, res: Response) => {
if (supabase) {
const response = await supabase.from('Projects').select('*').eq('orgId', orgId);
if (response && response.status == 200) {
res.status(200).json({
return res.status(200).json({
data: response.data,
});
} else {
res.status(400).send({ message: 'No Emailing tasks found for this organization' });
return res.status(400).send({ message: 'No Emailing tasks found for this organization' });
}
} else {
res.status(500).send({ message: 'Invalid Supabase Credentials' });
return res.status(500).send({ message: 'Invalid Supabase Credentials' });
}
} catch (e: any) {
console.error(e);
res.status(400).send({ message: e });
return res.status(400).send({ message: e });
}
};

export const addNewRecipient = async (req: Request, res: Response) => {
const { projectId, name, email, payload } = req.body;
if (!projectId || !name || !email || !payload) {
return res.status(400).send({ message: 'Missing required fields' });
}
if (supabase) {
const recipientExists = await supabase
.from('Recipients')
.select('*')
.eq('projectId', projectId)
.eq('email', email);
if (recipientExists && recipientExists.status == 200) {
if (recipientExists.data && recipientExists.data?.length > 0) {
res.status(200).json({ message: 'User already exists, add another user' });
} else {
const response = await supabase
.from('Recipients')
.insert({ name: name, email: email, payload: payload, projectId: projectId });
console.log('Insert:', response.data);
if (response && response.status == 200) {
res.status(200).json({ message: 'User successfully Added' });
try {
const { projectId, name, email, payload } = req.body;
if (!projectId || !name || !email || !payload) {
return res.status(400).send({ message: 'Missing required fields' });
}
if (supabase) {
const recipientExists = await supabase
.from('Recipients')
.select('*')
.eq('projectId', projectId)
.eq('email', email);
if (recipientExists && recipientExists.status == 200) {
if (recipientExists.data && recipientExists.data?.length > 0) {
return res.status(200).json({ message: 'User already exists, add another user' });
} else {
res.status(400).send({ message: 'User Not added', ...response.error });
const response = await supabase
.from('Recipients')
.insert({ name: name, email: email, payload: payload, projectId: projectId });
console.log('Insert:', response.data);
if (response && response.status == 200) {
return res.status(200).json({ message: 'User successfully Added' });
} else {
return res.status(400).send({ message: 'User Not added', ...response.error });
}
}
} else {
return res.status(400).send({ message: 'Supabase Error' });
}
} else {
res.status(400).send({ message: 'Supabase Error' });
return res.status(500).send({ message: 'Invalid Supabase Credentials' });
}
} else {
res.status(500).send({ message: 'Invalid Supabase Credentials' });
} catch (e: any) {
console.error(e);
return res.status(400).send({ message: e });
}
};
const generateOTP = () => {
// Generates a 6-digit random number
return Math.floor(100000 + Math.random() * 900000);
};
export const sendOTP = async (req: Request, res: Response) => {
try {
const { email, name, html } = req.body;
if (!email || !name || !html) {
return res.status(400).send({ message: 'Missing required fields' });
}
if (supabase) {
const response = await supabase.from('Otp').select('*').eq('email', email);
if (response && response.data && response.status == 200) {
if (response.data.length > 0) {
const otp = generateOTP();
const otpUpdateResponse = await supabase
.from('Otp')
.update({ code: otp })
.eq('email', email);
if (otpUpdateResponse && otpUpdateResponse.status == 200) {
const form = new FormData();
form.append('name', name);
form.append('to', email);
form.append('subject', 'Confirm your OTP');
let emailText: string = html;
emailText = emailText.replace('{{otp}}', otp.toString());
form.append('html', emailText);
form.append('text', 'Confirm your OTP');
const response = await axios.post(`${MAILER_URL}/mail`, form, {
headers: {
...form.getHeaders(),
authorization: AUTHORIZATION_TOKEN,
},
});
console.log(response.data);
if (response.status == 200) {
return res.status(200).json({
message: 'OTP sucessfully sent',
});
} else {
return res.status(400).json({
message: 'Failed: OTP email not sent',
});
}
} else {
return res
.status(400)
.send({ message: 'OTP not sent', error: response && response?.data });
}
} else {
const otp = generateOTP();
const newOtpResponse = await supabase.from('Otp').insert({ code: otp, email: email });
if (newOtpResponse && newOtpResponse.status == 200) {
const form = new FormData();
form.append('name', name);
form.append('to', email);
form.append('subject', 'Confirm your OTP');
let emailText: string = html;
emailText = emailText.replace('{{otp}}', otp.toString());
form.append('html', emailText);
form.append('text', 'Confirm your OTP');
const response = await axios.post(`${MAILER_URL}/mail`, form, {
headers: {
...form.getHeaders(),
authorization: AUTHORIZATION_TOKEN,
},
});
console.log(response.data);
if (response.status == 200) {
return res.status(200).json({
message: 'OTP sucessfully sent',
});
} else {
return res.status(400).json({
message: 'Failed: OTP email not sent',
});
}
} else {
return res
.status(400)
.send({ message: 'OTP not sent', error: response && response?.data });
}
}
} else {
return res.status(400).send({ message: 'supabase OTP error', ...response.error });
}
} else {
return res.status(500).send({ message: 'Invalid Supabase Credentials' });
}
} catch (e: any) {
console.error(e);
res.status(400).send({ message: e });
return res.status(400).send({ message: e });
}
};

export const verifyOTP = async (req: Request, res: Response) => {
try {
const { email, otp } = req.body;
if (!email || !otp) {
return res.status(400).send({ message: 'Missing required fields' });
}
if (supabase) {
const verifyResponse = await supabase.from('Otp').select('*').eq('email', email);
if (verifyResponse && verifyResponse.status == 200) {
if (verifyResponse.data) {
if (verifyResponse.data[0].code == otp) {
return res.send(200).json({
verified: true,
});
} else {
return res.send(200).json({
verified: false,
});
}
} else {
return res.status(400).send({ message: 'Email OTP not found - Supabase error' });
}
} else {
return res.status(400).send({ message: 'Email OTP not found - Supabase error' });
}
} else {
return res.status(500).send({ message: 'Invalid Supabase Credentials' });
}
} catch (e) {
console.error(e);
return res.status(400).send({ message: e });
}
};
28 changes: 19 additions & 9 deletions apps/core-admin/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ import { fetchAccountDetails, myCredential, updateAccountDetails } from './contr
import { validateOrganizationUser, validateOrganizationAdmin } from './middlewares/authorization';
import { addNewExtra, checkInExtra, getAllExtras, getExtraById } from './controllers/extras';
import { validateUUID } from './middlewares/validateParams';
import { addNewRecipient, getMailProjects, getMailStatus, newMailProject, sendMailWithQR } from './controllers/mail';
import {
addNewRecipient,
getMailProjects,
getMailStatus,
newMailProject,
sendMailWithQR,
sendOTP,
verifyOTP,
} from './controllers/mail';

const router: Router = express.Router();

Expand Down Expand Up @@ -110,12 +118,14 @@ router.get('/organizations/:orgId/events/:eventId/extras/:extraId', getExtraById
router.post('/organizations/:orgId/events/:eventId/extras/:extraId/check-in', checkInExtra);
router.post('/organizations/:orgId/events/:eventId/extras', addNewExtra);



//mailer routes
router.post('/organizations/:orgId/newEmailProject', newMailProject)
router.get('/organizations/:orgId/getEmailProjects', getMailProjects)
router.get('/organizations/:orgId/getMailStatus', getMailStatus)
router.post('/organizations/:orgId/addNewRecipient', addNewRecipient)
router.post('/organizations/:orgId/events/:eventId/mailQR', sendMailWithQR)
export default router;
router.post('/organizations/:orgId/newEmailProject', newMailProject);
router.get('/organizations/:orgId/getEmailProjects', getMailProjects);
router.get('/organizations/:orgId/getMailStatus', getMailStatus);
router.post('/organizations/:orgId/addNewRecipient', addNewRecipient);
router.post('/organizations/:orgId/events/:eventId/mailQR', sendMailWithQR);

// OTP routes
router.post('/organizations/sendOTP', sendOTP);
router.post('/organizations/verifyOTP', verifyOTP);
export default router;
5 changes: 4 additions & 1 deletion apps/core-admin/src/utils/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ dotenv.config();
const SUPABASE_URL = process.env.SUPABASE_URL;
const SUPABASE_KEY = process.env.SUPABASE_KEY;

const supabase = SUPABASE_URL !== undefined && SUPABASE_KEY !== undefined && createClient<Database>(SUPABASE_URL, SUPABASE_KEY);
const supabase =
SUPABASE_URL !== undefined &&
SUPABASE_KEY !== undefined &&
createClient<Database>(SUPABASE_URL, SUPABASE_KEY);

// module.exports = supabase;
export default supabase;
Loading

0 comments on commit 99b8c35

Please sign in to comment.