Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 73 additions & 5 deletions endpoints/slack-invite.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { client } = require('../app')
const { inviteUser } = require('../util/invite-user')
const { transcript } = require('../util/transcript')

module.exports = async function slackInvite(req, res) {
// this endpoint is hit by the form on hackclub.com/slack
Expand All @@ -13,13 +15,79 @@ module.exports = async function slackInvite(req, res) {
const email = req?.body?.email
const result = { email }
if (email) {
const { ok, error } = await inviteUser(req.body)
result.ok = ok
result.error = error
const res = await inviteUser(req.body)
result.ok = res.ok
result.error = res.error
let invites = res?.invites
if (invites) {
result.error = invites[0]?.error
result.ok = result.ok
}
if (result.error === 'already_in_team') {
// User is already in Slack - send them an email via Loops telling them how to login
let email = res?.invites[0]?.email
let userInfo = await client.users.lookupByEmail({ email })
let isMcg = userInfo?.user?.is_restricted
let isScg = userInfo?.user?.is_ultra_restricted
let dataVariables
let transactionalId
if (isMcg) {
// Check if they're in cave channel, invite if not
await client.conversations
.invite({
channel: transcript('channels.cave'),
users: userInfo.user.id,
token: process.env.SLACK_USER_TOKEN,
})
.catch((err) => {
if (err.data.error === 'already_in_channel') {
// User is already in channel, do nothing
} else {
console.log(err)
}
})
let caveChannelData = await client.conversations.info({
channel: transcript('channels.cave'),
})
let caveChannelName = caveChannelData.channel.name

dataVariables = {
email,
caveChannelName: caveChannelName,
caveChannelUrl: `https://hackclub.slack.com/archives/${transcript('channels.cave')}`,
}
transactionalId = process.env.LOOPS_MCG_TRANSACTIONAL_ID
} else if (isScg) {
// We shouldn't really have any of these and if we do, we likely don't want to promote them
return res.status(500).json({
ok: false,
error: 'User is already in Slack but is an SCG',
})
} else {
// Full user, don't invite to chanel - just set the full user template
dataVariables = {
email,
}
transactionalId = process.env.LOOPS_FULL_USER_TRANSACTIONAL_ID
}

const lres = await fetch('https://app.loops.so/api/v1/transactional', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.LOOPS_API_KEY}`,
},
body: JSON.stringify({
email,
transactionalId,
dataVariables,
}),
})
}
}
res.json(result)
return res.json(result)
} catch (e) {
console.log(e)
res.status(500).json({ ok: false, error: 'a fatal error occurred' })
return res.status(500).json({ ok: false, error: 'a fatal error occurred' })
}
}
1 change: 0 additions & 1 deletion util/invite-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ async function inviteGuestToSlack({ email, channels, _customMessage }) {
headers.append('Cookie', cookieValue)
headers.append('Content-Type', 'application/json')
headers.append('Authorization', `Bearer ${process.env.SLACK_BROWSER_TOKEN}`)

const data = JSON.stringify({
token: process.env.SLACK_BROWSER_TOKEN,
invites: [
Expand Down