Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db(🥔): limit db query #24

Merged
merged 3 commits into from
Nov 30, 2022
Merged
Changes from 1 commit
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
47 changes: 30 additions & 17 deletions slack-service/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const newUTCDate = () => {
return new Date(nowUtc)
}

const currentUTCDateDay = () => {

/// Find the user in the DB that has a matching slack ID, if there is none return null
async function getUserBySlackId(slackId) {
return await prisma.users.findFirst({
Expand Down Expand Up @@ -82,50 +84,61 @@ async function addMessage(senderDbId, receiverDbId, potatoCount) {
/// Gets the total potatoes given by the sender
async function getTotalPotatoesGiven(senderId) {
// Need to check if it not the same
const entry = await prisma.messages.findMany({
const entries = await prisma.messages.findMany({
where: {
sender_user_id: senderId,
},
});
return entry

return entries
.map((t) => t.amount)
.reduce((a, b) => a + b, 0);
}

/// Gets the total potatoes received by the sender
async function getTotalPotatoesReceived(senderId) {
async function getTotalPotatoesEverReceived(senderId) {
// Need to check if it not the same
const entry = await prisma.messages.findMany({
const entries = await prisma.messages.findMany({
where: {
receiver_user_id: senderId,
},
});

return entry
if(!entries.length){
return 0
}

return entries
.map((t) => t.amount)
.reduce((a, b) => a + b, 0);
}

/// Gets the potatoes given today by the sender
async function getPotatoesGivenToday(senderId) {
// Need to check if it not the same
const entry = await prisma.messages.findMany({
const entries = await prisma.messages.findMany({
select: {
amount: true,
},
// You can only ever send 5 potato per day, so it takes at most 5 messages
// before you run out of potatoes. We can just take last 5 messages from today and discard the rest
take: maxPotato,
orderBy: [{
created: 'desc'
}],
where: {
sender_user_id: senderId,
created: {
gte: newUTCDate().setHours(0, 0, 0, 0),
}
},
});

const datesAreOnSameDay = (first, second) =>
first.getUTCFullYear() === second.getUTCFullYear() &&
first.getUTCMonth() === second.getUTCMonth() &&
first.getUTCDate() === second.getUTCDate();
if(!entries.length) {
return 0;
}

const cur = newUTCDate();
return entry
.filter((t) => datesAreOnSameDay(t.created, cur))
.map((t) => t.amount)
.reduce((a, b) => a + b, 0);
return entries.map((t) => t.amount).reduce((a, b) => a + b, 0);
}

/// Get the full name using the userId from the Slack API
Expand Down Expand Up @@ -311,7 +324,7 @@ app.event("app_home_opened", async ({ event, client, context }) => {
const homePromises = [
getPotatoesGivenToday(userDbId),
getTotalPotatoesGiven(userDbId),
getTotalPotatoesReceived(userDbId)
getTotalPotatoesEverReceived(userDbId)
]

await Promise.all(homePromises).then(async ([potatoesGivenToday, totalPotatoesGiven, totalPotatoesReceived]) => {
Expand Down