Skip to content
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
2 changes: 1 addition & 1 deletion db/migrations/20240423115247-create-points-base-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ module.exports = {
async down (queryInterface, Sequelize) {
await queryInterface.dropTable("points_base");
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ module.exports = {
async down (queryInterface, Sequelize) {
await queryInterface.bulkDelete("points_base", {actionName: ["linkedin", "github"]});
}
};
};
22 changes: 22 additions & 0 deletions db/migrations/20240507101000-add-connect-email-to-points-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface, Sequelize) {

await queryInterface.bulkInsert("points_base",[
{
actionName: "connect_email",
pointsPerAction: 1,
scalingFactor: 1,
counter: "1",
createdAt: new Date(),
updatedAt: new Date(),
}
])
},

async down (queryInterface, Sequelize) {
await queryInterface.bulkDelete("points_base", {actionName: ["connect_email"]});
}
};
43 changes: 43 additions & 0 deletions db/migrations/20240507161929-change-default-points-base-values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface, Sequelize) {
const PointsBase = (actionName, pointsPerAction, counter, scalingFactor) => ({
actionName,
pointsPerAction,
counter,
scalingFactor
});

const initialRules = [
PointsBase("locked", 1, "N", 1),
PointsBase("delegated", 1, "N", 1),
PointsBase("created_marketplace", 1, "1", 1),
PointsBase("created_task", 1, "N", 1),
PointsBase("created_deliverable", 1, "1", 1),
PointsBase("created_proposal", 1, "1", 1),
PointsBase("accepted_proposal", 1, "1", 1),
PointsBase("add_linkedin", 10, "1", 1),
PointsBase("add_github", 10, "1", 1),
];

for (const rule of initialRules) {
await queryInterface.bulkUpdate("points_base", {
pointsPerAction: rule.pointsPerAction,
scalingFactor: rule.scalingFactor,
counter: rule.counter,
}, {
actionName: rule.actionName
});
}
},

async down (queryInterface, Sequelize) {
await queryInterface.bulkUpdate("points_base", {
pointsPerAction: 1,
scalingFactor: 1,
counter: "1",
});
}
};
20 changes: 20 additions & 0 deletions server/common/user/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { UserEmailErrors } from "server/errors/error-messages";
import { HttpBadRequestError, HttpConflictError } from "server/errors/http-errors";
import { emailService } from "server/services/email";
import { EmailTemplates } from "server/templates";
import { addPointEntry } from "server/utils/points-system/add-point-entry";
import { removePointEntry } from "server/utils/points-system/remove-point-entry";
import { updatePointEntryInfo } from "server/utils/points-system/update-point-entry";
import { TemplateProcessor } from "server/utils/template";

const {
Expand Down Expand Up @@ -65,6 +68,13 @@ export async function put(req: NextApiRequest) {
const { email, context } = req.body;
const { user } = context;

const connectEmailPointEvent = await models.pointsEvents.findOne({
where: {
userId: user.id,
actionName: "connect_email"
}
});

if (!email) {
user.email = null;
user.isEmailConfirmed = false;
Expand All @@ -73,6 +83,9 @@ export async function put(req: NextApiRequest) {

await user.save();

if (connectEmailPointEvent)
await removePointEntry(connectEmailPointEvent.id);

return;
}

Expand Down Expand Up @@ -113,4 +126,11 @@ export async function put(req: NextApiRequest) {
user.emailVerificationSentAt = new Date();

await user.save();

try {
await addPointEntry(user.id, "connect_email", { value: email });
} catch(error) {
if (connectEmailPointEvent)
await updatePointEntryInfo(connectEmailPointEvent.id, { value: email });
}
}
3 changes: 2 additions & 1 deletion server/utils/points-system/add-point-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {Op} from "sequelize";

import Database from "db/models";

import {Logger} from "../../../services/logging";
import {Logger} from "services/logging";

import {PointEventAction} from "../../../types/point-event-action";

export async function addPointEntry(userId: number, actionName: PointEventAction, info = {}) {
Expand Down
19 changes: 19 additions & 0 deletions server/utils/points-system/update-point-entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import models from "db/models";

import { Logger } from "services/logging";

export async function updatePointEntryInfo(pointEntryId: number, info: object) {
const pointEntry = await models.pointsEvents.findOne({
where: {
id: pointEntryId
}
});

if (!pointEntry)
throw new Error(`Entry with id ${pointEntryId} not found on points_events`);

pointEntry.info = info;
await pointEntry.save();

Logger.info(`PointsEvents ${pointEntryId} updated with ${info}`);
}
3 changes: 2 additions & 1 deletion types/point-event-action.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export type PointEventAction =
"accepted_proposal" |
"add_github" |
"add_linkedin" |
"add_about";
"add_about" |
"connect_email";