Created
November 8, 2021 08:38
-
-
Save wontwon/74b605d0743e8b7a3e77afadd43d7b54 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fetch = require("node-fetch"); | |
| import { GraphQLClient } from "graphql-request"; | |
| const { customAlphabet } = require("nanoid"); | |
| import { CREATE_GREETING_MUTATION, GET_USER_QUERY } from "../Queries"; | |
| const alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv"; | |
| const sgMail = require("@sendgrid/mail"); | |
| module.exports = async (req, res) => { | |
| const client = new GraphQLClient(process.env.NHOST_BACKEND_URL, { | |
| headers: { | |
| ["x-hasura-admin-secret"]: process.env.NHOST_ADMIN_SECRET, | |
| }, | |
| }); | |
| const userId = req.body.session_variables["x-hasura-user-id"]; | |
| const { | |
| recipient_name, | |
| sender_name, | |
| occasion_id, | |
| prompt_id, | |
| custom_prompt, | |
| is_group, | |
| price_id, | |
| } = req.body.input.object; | |
| const shortId = customAlphabet(alphabet, 6); | |
| const editId = shortId(); | |
| const variables = { | |
| recipient_name, | |
| sender_name, | |
| occasion_id, | |
| prompt_id, | |
| custom_prompt, | |
| edit_id: editId, | |
| user_id: userId, | |
| is_group, | |
| price_id, | |
| }; | |
| const fetchRes = await client.request(CREATE_GREETING_MUTATION, variables); | |
| const { insert_greetings_one } = fetchRes; | |
| const { edit_id } = insert_greetings_one; | |
| if (edit_id) { | |
| const awwdioPath = `${process.env.APP_PORT}/create/${variables.edit_id}`; | |
| const userRes = await client.request(GET_USER_QUERY, { | |
| user_id: userId, | |
| }); | |
| const userObj = userRes.users[0]; | |
| const { account } = userObj; | |
| await sgMail.send({ | |
| to: account.email, // Change to your recipient | |
| from: "[email protected]", // Change to your verified sender | |
| templateId: "d-ef51ee0799da4ad0b90a332874a03c36", | |
| dynamicTemplateData: { | |
| recipientName: recipient_name, | |
| senderName: sender_name, | |
| editPath: awwdioPath, | |
| }, | |
| }); | |
| fetch( | |
| "https://hooks.slack.com/services/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", | |
| { | |
| method: "post", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ | |
| text: `You ain't a scrub, Tuanzy. ${sender_name} has just created a ${ | |
| is_group ? "group" : "solo" | |
| } greeting for ${recipient_name}`, | |
| }), | |
| } | |
| ); | |
| return res.status(200).send({ | |
| edit_id, | |
| }); | |
| } else { | |
| return res.status(400).json({ | |
| error: "Error creating greeting", | |
| }); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment