Skip to content

Commit

Permalink
Contact Functionality integrated with spreadsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
sayshark75 committed Jan 15, 2025
1 parent 7023c1c commit 4f673f9
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@chakra-ui/react": "^3.1.1",
"@emotion/react": "^11.13.3",
"framer-motion": "^11.11.11",
"googleapis": "^144.0.0",
"next": "15.0.3",
"next-themes": "^0.4.3",
"nodemailer": "^6.9.16",
Expand Down
3 changes: 3 additions & 0 deletions src/app/api/contact/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import getCurrentDateInTimeZone from "@/utils/getCurrentDatetime";
import { googleSheetsUtil } from "@/utils/googleSheetsUtil";
import { verifyRecaptcha } from "@/utils/recaptchaUtils";
import { NextRequest, NextResponse } from "next/server";
import nodemailer from "nodemailer";
Expand Down Expand Up @@ -56,8 +57,10 @@ export async function POST(req: NextRequest) {
text: `From:\nUser - ${name}, \nEmail - ${email},\n\nMessage - ${message}\n\n${getCurrentDateInTimeZone("Asia/Kolkata")}`,
html: "",
});
await googleSheetsUtil("A1:C1", [[name, email, message]]);
return NextResponse.json({ message: "mail sent successfully", status: true, data: info });
} catch (error) {
console.log("error: ", error);
return NextResponse.json({ message: "mail not sent", status: false, data: null });
}
}
24 changes: 24 additions & 0 deletions src/utils/googleSheetsUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { google } from "googleapis";
export const googleSheetsUtil = async (range: string, values: any[][]) => {
const auth = new google.auth.GoogleAuth({
credentials: {
client_email: process.env.GOOGLE_CLIENT_EMAIL,
private_key: process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, "\n"),
},
scopes: ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/spreadsheets"],
});

const sheets = google.sheets({
auth,
version: "v4",
});

return await sheets.spreadsheets.values.append({
spreadsheetId: process.env.GOOGLE_CONTACT_SHEET_ID,
range,
valueInputOption: "USER_ENTERED",
requestBody: {
values,
},
});
};
Loading

0 comments on commit 4f673f9

Please sign in to comment.