Skip to content

Commit

Permalink
subscribe task, proposal, deliverable creator to task
Browse files Browse the repository at this point in the history
  • Loading branch information
vhcsilva committed Jul 26, 2024
1 parent aa84209 commit 71eae9b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/actions/get-bounty-created-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {Push} from "../services/analytics/push";
import {AnalyticEventName} from "../services/analytics/types/events";
import {getDeveloperAmount} from "src/modules/calculate-distributed-amounts";
import {getCoinIconByChainAndContractAddress} from "src/services/coingecko";
import { subscribeUserToTask } from "src/utils/notifications/subscribe-user-to-task";


export const name = "getBountyCreatedEvents";
Expand Down Expand Up @@ -148,6 +149,8 @@ export async function action(block: DecodedLog<BountyCreatedEvent['returnValues'

await dbBounty.save();

await subscribeUserToTask(dbBounty.id, dbBounty.user.address!);

updateLeaderboardBounties();
updateBountiesHeader();

Expand Down
3 changes: 3 additions & 0 deletions src/actions/get-proposal-created-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {NETWORK_NOT_FOUND} from "../utils/messages.const";
import {Push} from "../services/analytics/push";
import {AnalyticEventName} from "../services/analytics/types/events";
import updateSeoCardBounty from "src/modules/handle-seo-card";
import { subscribeUserToTask } from "src/utils/notifications/subscribe-user-to-task";

export const name = "getBountyProposalCreatedEvents";
export const schedule = "*/13 * * * *";
Expand Down Expand Up @@ -88,6 +89,8 @@ export async function action(block: DecodedLog<BountyProposalCreatedEvent['retur
sendMessageToTelegramChannels(BOUNTY_STATE_CHANGED(dbBounty.state, dbBounty));
}

await subscribeUserToTask(dbBounty.id, dbBounty.user.address!);

updateLeaderboardProposals();
updateSeoCardBounty(dbBounty.id, name);

Expand Down
7 changes: 5 additions & 2 deletions src/actions/get-pullrequest-created-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {DELIVERABLE_OPEN} from "../integrations/telegram/messages";
import {Push} from "../services/analytics/push";
import {AnalyticEventName} from "../services/analytics/types/events";
import updateSeoCardBounty from "src/modules/handle-seo-card";
import { subscribeUserToTask } from "src/utils/notifications/subscribe-user-to-task";

export const name = "getBountyPullRequestCreatedEvents";
export const schedule = "*/10 * * * *";
Expand All @@ -33,7 +34,7 @@ export async function action(block: DecodedLog<BountyPullRequestCreatedEvent['re

const dbBounty = await db.issues.findOne({
where: {contractId: bountyId, network_id: network.id},
include: [{association: "network"}, {association: "chain"}]
include: [{association: "network"}, {association: "chain"}, {association: "user"}]
});

if (!dbBounty) {
Expand Down Expand Up @@ -61,6 +62,8 @@ export async function action(block: DecodedLog<BountyPullRequestCreatedEvent['re
dbDeliverable.bountyId = bounty.id
await dbDeliverable.save();

await subscribeUserToTask(dbBounty.id, dbBounty.user.address!);

updateSeoCardBounty(dbBounty.id, name);

eventsProcessed[network.name!] = {
Expand All @@ -80,7 +83,7 @@ export async function action(block: DecodedLog<BountyPullRequestCreatedEvent['re
}
}

Push.events([AnalyticEvent])
Push.events([AnalyticEvent]);

return eventsProcessed;
}
2 changes: 1 addition & 1 deletion src/modules/proposal-validate-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default async function validateProposalState(currentState: string,
export async function validateProposal(bounty: Bounty, prId: number, proposalId: number, network_id: number, isProposalRequired = true) {
const dbBounty = await db.issues.findOne({
where: {contractId: bounty.id, network_id},
include: [ { association: "network" }, {association: "chain"}]
include: [ { association: "network" }, {association: "chain"}, {association: "user"}]
})
if (!dbBounty)
return logger.error(DB_BOUNTY_NOT_FOUND('validate-proposal', bounty.cid, network_id));
Expand Down
45 changes: 45 additions & 0 deletions src/utils/notifications/subscribe-user-to-task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Sequelize, WhereOptions } from "sequelize";

import models from "src/db";
import logger from "src/utils/logger-handler";

export async function subscribeUserToTask(taskId: number, userAddress: string) {
const where: WhereOptions = {
address: Sequelize.where(Sequelize.fn("lower", Sequelize.col("address")), userAddress?.toLowerCase()),
};

const user = await models.users.findOne({ where });

if (!user) {
logger.warn(`Failed to subscribeUserToTask: user not found`, { taskId, userAddress });
return;
}

const task = await models.issues.findOne({
where: {
id: taskId
}
});

if (!task) {
logger.warn(`Failed to subscribeUserToTask: task not found`, { taskId, userAddress });
return;
}

const settings = await models.notification_settings.findOne({
where: {
userId: user.id
}
});


if (!settings?.subscriptions) {
logger.warn(`Failed to subscribeUserToTask: notification settings not found`, { taskId, userAddress });
return;
}

if (!settings.subscriptions.includes(taskId))
await settings.update({
subscriptions: [...settings.subscriptions, taskId]
});
}

0 comments on commit 71eae9b

Please sign in to comment.