Skip to content

Instantly share code, notes, and snippets.

@theskinnycoder
Created November 22, 2021 06:52
Show Gist options
  • Save theskinnycoder/36ec1b3f0a51fa5a52d88d543dd44967 to your computer and use it in GitHub Desktop.
Save theskinnycoder/36ec1b3f0a51fa5a52d88d543dd44967 to your computer and use it in GitHub Desktop.
import { auth, sheets } from '@googleapis/sheets'
import dotenv from 'dotenv'
dotenv.config()
const check = async (req, res) => {
switch (req.method) {
case 'GET': {
const authService = new auth.GoogleAuth({
projectId: process.env.GOOGLE_PROJECT_ID,
credentials: {
private_key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n'),
client_email: process.env.GOOGLE_CLIENT_EMAIL,
},
keyFilename: './service-account-keys.json',
scopes: ['https://www.googleapis.com/auth/spreadsheets.readonly'],
})
const authClient = await authService.getClient()
const sheetsClient = sheets({
version: 'v4',
auth: authClient,
})
const columnNameResults = await sheetsClient.spreadsheets.values.get({
spreadsheetId: req?.query?.id,
range: 'Sheet1!A1:J1',
})
const emailColumnNumber =
columnNameResults.data.values[0].indexOf('Email')
const nameColumnNumber = columnNameResults.data.values[0].indexOf('Name')
const result = await sheetsClient.spreadsheets.values.get({
spreadsheetId: req?.query?.id,
range: 'Sheet1',
})
const students = result.data.values.map((student) => ({
email: student[emailColumnNumber],
name: student[nameColumnNumber],
}))
const { email } = req?.body
if (students.map((student) => student.email).includes(email)) {
res.json({
status: 'success',
name: students.find((student) => student.email === email).name,
})
} else {
res.status(404).json({
status: 'error',
message: "You aren't registered to this workshop😞",
})
}
break
}
default:
return res.status(405).send('Method not allowed')
}
}
export { check as function }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment