Skip to content

Commit 66d3fe6

Browse files
committed
completed insertion
1 parent 7c9c535 commit 66d3fe6

File tree

9 files changed

+424
-83
lines changed

9 files changed

+424
-83
lines changed

models/badges/hack-r-play-2022.js

+64-71
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,22 @@
1+
import { HackRPlayBadges } from "../../services/badges.js";
2+
// import { sendMail } from "../../services/email/index.js";
13
import { submit } from "../../services/submit.js";
4+
import {
5+
GetAllParticipantIdeasMemberIdQuery,
6+
GetUserBadgeQuery,
7+
GetAllAuthorsIdQuery,
8+
GetAllCompletedIdeasIdQuery,
9+
GetAllWinnerUserIdQuery,
10+
InsertBadgeQuery,
11+
GetUserDetailsQuery,
12+
} from "./queries.js";
213

3-
const GetAllParticipantIdeasMemberId = () => {
4-
return {
5-
display: "Get All Participants",
6-
name: "hackathon_ideas_members",
7-
function: "hackathon_ideas_members",
8-
return: ["user_id", "idea_id"],
9-
};
10-
};
11-
12-
const GetAllAuthorsId = () => {
13-
return {
14-
display: "Get All Author",
15-
name: "hackathon_ideas",
16-
function: "hackathon_ideas",
17-
return: ["id", "owner"],
18-
};
19-
};
20-
21-
const GetAllCompletedIdeasId = () => {
22-
return {
23-
display: "Get All Submissions",
24-
name: "hackathon_idea_submission",
25-
function: "hackathon_idea_submission",
26-
return: ["idea_id"],
27-
};
28-
};
29-
30-
const GetAllWinnerUserId = () => {
31-
return {
32-
display: "Get Winners",
33-
name: "hackathon_winners",
34-
function: "hackathon_winners",
35-
return: ["user_id", "position_type"],
36-
};
37-
};
38-
39-
export const UpdateHackRPlayBadges = (url) => {
14+
export const UpdateHackRPlayBadges = (url, sendgrid_api_key) => {
4015
const promises = [
41-
submit(GetAllParticipantIdeasMemberId(), url),
42-
submit(GetAllCompletedIdeasId(), url),
43-
submit(GetAllWinnerUserId(), url),
44-
submit(GetAllAuthorsId(), url),
16+
submit(GetAllParticipantIdeasMemberIdQuery(), url),
17+
submit(GetAllCompletedIdeasIdQuery(), url),
18+
submit(GetAllWinnerUserIdQuery(), url),
19+
submit(GetAllAuthorsIdQuery(), url),
4520
];
4621
return Promise.all(promises)
4722
.then((res) => {
@@ -65,37 +40,38 @@ export const UpdateHackRPlayBadges = (url) => {
6540
completedIdeas
6641
);
6742
const winners = getAllWinnersId(allContributors, winningIdeas);
68-
// allContributors.forEach((cont) => {
69-
// if (participants.indexOf(cont.user_id) < 0) {
70-
// participants.push(cont.user_id);
71-
// }
43+
// console.log(`Participants : ${contributorsId.length}`);
44+
// console.log(contributorsId);
45+
// console.log(`Submitters: ${completedContributorsId.length}`);
46+
// console.log(completedContributorsId);
47+
// console.log(`Winners : ${winners.length}`);
48+
// console.log(winners);
49+
const submitPromises = [];
50+
contributorsId.forEach((cont) => {
51+
submitPromises.push(
52+
InsertBadge(cont, HackRPlayBadges.participant, url)
53+
);
54+
});
7255

73-
// const completedIdeaFilter = completedIdeas.filter(
74-
// (ci) => ci.idea_id === cont.idea_id
75-
// );
76-
// if (
77-
// completedIdeaFilter.length > 0 &&
78-
// submitters.indexOf(cont.user_id) < 0
79-
// ) {
80-
// submitters.push(cont.user_id);
81-
// }
82-
// });
83-
// winnersIdeas.forEach((wi) => {
84-
// if (wi.position_type.toLowerCase() === "w") {
85-
// winners.push(wi.user_id);
86-
// }
87-
// });
88-
console.log(`Participants : ${contributorsId.length}`);
89-
console.log(contributorsId);
90-
console.log(`Submitters: ${completedContributorsId.length}`);
91-
console.log(completedContributorsId);
92-
console.log(`Winners : ${winners.length}`);
93-
console.log(winners);
94-
return {
95-
winners: winners,
96-
submitters: completedContributorsId,
97-
participants: contributorsId,
98-
};
56+
completedContributorsId.forEach((cont) => {
57+
submitPromises.push(InsertBadge(cont, HackRPlayBadges.submitters, url));
58+
});
59+
60+
winners.forEach((cont) => {
61+
submitPromises.push(InsertBadge(cont, HackRPlayBadges.winners, url));
62+
});
63+
return Promise.all(submitPromises)
64+
.then((res) => {
65+
// sendMail(sendgrid_api_key);
66+
return {
67+
winners: winners,
68+
submitters: completedContributorsId,
69+
participants: contributorsId,
70+
};
71+
})
72+
.catch((err) => {
73+
// console.log(some)
74+
});
9975
})
10076
.catch((err) => {
10177
// console.error(err);
@@ -137,3 +113,20 @@ const getAllWinnersId = (allContributors, winningIdeas) => {
137113
});
138114
return winners;
139115
};
116+
117+
const IsUserBadgeExists = async (user_id, badge_id, url) => {
118+
const res = await submit(GetUserBadgeQuery(user_id, badge_id), url);
119+
console.log(res, res.length, res.length > 0);
120+
return res.length > 0;
121+
};
122+
123+
const InsertBadge = async (user_id, badge_id, url) => {
124+
if (!(await IsUserBadgeExists(user_id, badge_id, url))) {
125+
console.log(`Inserting user: ${user_id}`);
126+
const userDetails = await submit(GetUserDetailsQuery(user_id), url);
127+
console.log(userDetails);
128+
return submit(InsertBadgeQuery(user_id, badge_id), url);
129+
} else {
130+
console.log(`User badge exists: ${user_id}`);
131+
}
132+
};

models/badges/queries.js

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
export const GetAllParticipantIdeasMemberIdQuery = () => {
2+
return {
3+
display: "Get All Participants",
4+
name: "hackathon_ideas_members",
5+
function: "hackathon_ideas_members",
6+
return: ["user_id", "idea_id"],
7+
};
8+
};
9+
10+
export const GetUserDetailsQuery = (user_id) => {
11+
return {
12+
display: "Get User Details",
13+
name: "users",
14+
function: "users",
15+
where: {
16+
clause: {
17+
operator: "and",
18+
conditions: [
19+
{
20+
field: "id",
21+
operator: "eq",
22+
value: user_id,
23+
},
24+
],
25+
},
26+
},
27+
return: ["displayName", "email"],
28+
};
29+
};
30+
31+
export const GetUserBadgeQuery = (user_id, badge_id) => {
32+
return {
33+
display: "Get User Badge Map",
34+
name: "meta_user_badge_map",
35+
function: "meta_user_badge_map",
36+
where: {
37+
clause: {
38+
operator: "and",
39+
conditions: [
40+
{
41+
field: "user_id",
42+
operator: "eq",
43+
value: user_id,
44+
},
45+
{
46+
field: "badge_id",
47+
operator: "eq",
48+
value: badge_id,
49+
},
50+
],
51+
},
52+
},
53+
return: ["id"],
54+
};
55+
};
56+
57+
export const GetAllAuthorsIdQuery = () => {
58+
return {
59+
display: "Get All Author",
60+
name: "hackathon_ideas",
61+
function: "hackathon_ideas",
62+
return: ["id", "owner"],
63+
};
64+
};
65+
66+
export const GetAllCompletedIdeasIdQuery = () => {
67+
return {
68+
display: "Get All Submissions",
69+
name: "hackathon_idea_submission",
70+
function: "hackathon_idea_submission",
71+
return: ["idea_id"],
72+
};
73+
};
74+
75+
export const GetAllWinnerUserIdQuery = () => {
76+
return {
77+
display: "Get Winners",
78+
name: "hackathon_winners",
79+
function: "hackathon_winners",
80+
return: ["user_id", "position_type"],
81+
};
82+
};
83+
84+
export const InsertBadgeQuery = (badge_id, user_id) => {
85+
return {
86+
display: "Get Winners",
87+
name: "insert_meta_user_badges_one",
88+
function: "insert_meta_user_badges_one",
89+
write: true,
90+
object: {
91+
badge_id: badge_id,
92+
user_id: user_id,
93+
},
94+
return: ["id"],
95+
};
96+
};

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
"type": "module",
1313
"dependencies": {
1414
"@nhost/nhost-js": "^1.5.2",
15+
"@sendgrid/mail": "^7.7.0",
1516
"dotenv": "^16.0.3",
1617
"express": "^4.17.1",
17-
"json-graphql-parser": "^0.1.9"
18+
"json-graphql-parser": "^0.1.9",
19+
"nodemailer": "^6.8.0",
20+
"nodemailer-express-handlebars": "^5.0.0"
1821
}
1922
}

server.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as dotenv from "dotenv";
22
dotenv.config();
33
import express from "express";
44
import { UpdateHackRPlayBadges } from "./models/badges/hack-r-play-2022.js";
5+
import { sendMail } from "./services/email/index.js";
56

67
// Configuration
78
var app = express();
@@ -12,6 +13,7 @@ if (process.env.SERVER_PORT) {
1213
}
1314

1415
const BACKEND_URL = `${process.env.NHOST_BACKEND_URL}/${process.env.NHOST_VERSION}/${process.env.NHOST_ENDPOINT}`;
16+
const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY;
1517
console.log(BACKEND_URL);
1618

1719
app.get("/", function (req, res) {
@@ -23,7 +25,7 @@ app.get("/", function (req, res) {
2325

2426
app.post("/badges", function (req, res) {
2527
res.writeHead(200, { "Content-Type": "application/json" });
26-
UpdateHackRPlayBadges(BACKEND_URL).then((result) => {
28+
UpdateHackRPlayBadges(BACKEND_URL, SENDGRID_API_KEY).then((result) => {
2729
var response = {
2830
response: "Successfully updated badges.",
2931
};
@@ -32,6 +34,16 @@ app.post("/badges", function (req, res) {
3234
});
3335
});
3436

37+
app.post("/mail", function (req, res) {
38+
res.writeHead(200, { "Content-Type": "application/json" });
39+
sendMail();
40+
var response = {
41+
response: "Successfully sent email.",
42+
};
43+
console.log(response);
44+
res.end(JSON.stringify({ result: "done" }));
45+
});
46+
3547
var server = app.listen(app.get("port"), function () {
3648
var host = server.address().address;
3749
var port = server.address().port;

services/badges.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const HackRPlayBadges = {
2+
participant: "000d34a9-b675-47f9-80c3-751660970f28",
3+
submitters: "000d34a9-b675-47f9-80c3-751660970f29",
4+
winners: "000d34a9-b675-47f9-80c3-751660970f30",
5+
};

services/email/index.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import hbs from "nodemailer-express-handlebars";
2+
import nodemailer from "nodemailer";
3+
import path from "path";
4+
5+
// initialize nodemailer
6+
var transporter = nodemailer.createTransport({
7+
service: "gmail",
8+
auth: {
9+
10+
pass: "password_for_your_email_address",
11+
},
12+
});
13+
14+
// point to the template folder
15+
const handlebarOptions = {
16+
viewEngine: {
17+
partialsDir: path.resolve("./views/"),
18+
defaultLayout: false,
19+
},
20+
viewPath: path.resolve("./views/"),
21+
};
22+
23+
// use a template file with nodemailer
24+
transporter.use("compile", hbs(handlebarOptions));
25+
26+
export const sendMail = () => {
27+
var mailOptions = {
28+
from: '"Adebola" <[email protected]>', // sender address
29+
to: "[email protected]", // list of receivers
30+
subject: "Welcome!",
31+
template: "email", // the name of the template file i.e email.handlebars
32+
context: {
33+
name: "Adebola", // replace {{name}} with Adebola
34+
company: "My Company", // replace {{company}} with My Company
35+
},
36+
};
37+
38+
// trigger the sending of the E-mail
39+
transporter.sendMail(mailOptions, function (error, info) {
40+
if (error) {
41+
return console.log(error);
42+
}
43+
console.log("Message sent: " + info.response);
44+
});
45+
};
46+
47+
// import sgMail from "@sendgrid/mail";
48+
49+
// export const sendMail = (sendgrid_api_key) => {
50+
// sgMail.setApiKey(sendgrid_api_key);
51+
// const msg = {
52+
// to: "[email protected]", // Change to your recipient
53+
// from: "[email protected]", // Change to your verified sender
54+
// subject: "Sending with SendGrid is Fun",
55+
// text: "and easy to do anywhere, even with Node.js",
56+
// html: "<strong>and easy to do anywhere, even with Node.js</strong>",
57+
// };
58+
// sgMail
59+
// .send(msg)
60+
// .then(() => {
61+
// console.log("Email sent");
62+
// })
63+
// .catch((error) => {
64+
// console.error(error);
65+
// });
66+
// };

0 commit comments

Comments
 (0)