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

chore(ew): update channels where to send the report #1768

Merged
merged 2 commits into from
Oct 28, 2024
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
24 changes: 6 additions & 18 deletions apps/epic-web/src/inngest/functions/stripe/slack-daily-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ interface SlackChannelIds {

interface Contributor {
userId: string
saleAnnounceChannel: string
slackChannel: string
}

const OWNER_USER_ID = '4ef27e5f-00b4-4aa3-b3c4-4a58ae76f50b'

export const slackDailyReporter = inngest.createFunction(
{
id: 'stripe/slack-daily-reporter',
Expand Down Expand Up @@ -157,29 +155,19 @@ export const slackDailyReporter = inngest.createFunction(

const query = groq`*[_type == "contributor" && userId in $userIds] {
userId,
saleAnnounceChannel
slackChannel
}`

const contributors: Contributor[] = await sanityClient.fetch(query, {
userIds,
})

const slackChannelMap = contributors.reduce<SlackChannelIds>(
const slackIds: SlackChannelIds = contributors.reduce<SlackChannelIds>(
(acc, contributor) => {
if (contributor.userId && contributor.saleAnnounceChannel) {
acc[contributor.userId] = contributor.saleAnnounceChannel
}
return acc
},
{},
)

const slackIds: SlackChannelIds = userIds.reduce<SlackChannelIds>(
(acc, userId) => {
if (userId === OWNER_USER_ID) {
acc[userId] = process.env.SLACK_ANNOUNCE_CHANNEL_ID || null
if (contributor.userId && contributor.slackChannel) {
acc[contributor.userId] = contributor.slackChannel
} else {
acc[userId] = slackChannelMap[userId] || null
acc[contributor.userId] = null
}
return acc
},
Expand Down
38 changes: 15 additions & 23 deletions apps/epic-web/src/inngest/functions/stripe/slack-weekly-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ interface SlackChannelIds {

interface Contributor {
userId: string
saleAnnounceChannel: string
slackChannel: string
}

const OWNER_USER_ID = '4ef27e5f-00b4-4aa3-b3c4-4a58ae76f50b'

export const slackWeeklyReporter = inngest.createFunction(
{
id: 'stripe/slack-weekly-reporter',
Expand Down Expand Up @@ -159,29 +157,19 @@ export const slackWeeklyReporter = inngest.createFunction(

const query = groq`*[_type == "contributor" && userId in $userIds] {
userId,
saleAnnounceChannel
slackChannel
}`

const contributors: Contributor[] = await sanityClient.fetch(query, {
userIds,
})

const slackChannelMap = contributors.reduce<SlackChannelIds>(
const slackIds: SlackChannelIds = contributors.reduce<SlackChannelIds>(
(acc, contributor) => {
if (contributor.userId && contributor.saleAnnounceChannel) {
acc[contributor.userId] = contributor.saleAnnounceChannel
}
return acc
},
{},
)

const slackIds: SlackChannelIds = userIds.reduce<SlackChannelIds>(
(acc, userId) => {
if (userId === OWNER_USER_ID) {
acc[userId] = process.env.SLACK_ANNOUNCE_CHANNEL_ID || null
if (contributor.userId && contributor.slackChannel) {
acc[contributor.userId] = contributor.slackChannel
} else {
acc[userId] = slackChannelMap[userId] || null
acc[contributor.userId] = null
}
return acc
},
Expand Down Expand Up @@ -317,6 +305,12 @@ export const slackWeeklyReporter = inngest.createFunction(
)}, and ${lastProduct}`
}

const monthYearText = new Date(
new Date().getFullYear(),
new Date().getMonth(),
1,
).toLocaleDateString('en-US', {month: 'long', year: 'numeric'})

summaryMessage += ` for an estimated royalty of ${formatCurrency(
userTotalRevenueThisMonth,
)}.`
Expand All @@ -326,17 +320,15 @@ export const slackWeeklyReporter = inngest.createFunction(
type: 'header',
text: {
type: 'plain_text',
text: `Daily Revenue Report for ${userName} 💰`,
text: `Weekly Revenue Report for ${userName} 💰`,
emoji: true,
},
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Report Date: ${new Date(
Date.now() - 86400000,
).toLocaleDateString()}*`,
text: `*Month-to-date Report - ${monthYearText}*`,
},
},
{
Expand Down Expand Up @@ -364,7 +356,7 @@ export const slackWeeklyReporter = inngest.createFunction(
await postToSlack({
channel: targetChannelId,
webClient,
text: `Daily Revenue Report for ${userName}`,
text: `Weekly Revenue Report for ${userName}`,
blocks,
})
results.push({userId, channelId: targetChannelId, success: true})
Expand Down
Loading