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

Deploying to prod #39

Merged
merged 73 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
7452b33
refactor: Update UpdateActivityUsecase to validate and update status_…
FelipeCarillo May 21, 2024
4404a85
refactor: Update language codes in LanguageMock
FelipeCarillo May 21, 2024
b2c4a30
refactor: Update EventBridgeManager to enable rule state
FelipeCarillo May 21, 2024
f7661db
refactor: Update CreateActivityUsecase to handle time zone offset
FelipeCarillo May 21, 2024
730bfb7
refactor: Update UpdateActivityUsecase to handle time zone offset and…
FelipeCarillo May 21, 2024
084a168
refactor: Update UpdateActivityEventPresenter to handle activity stat…
FelipeCarillo May 21, 2024
e9d7587
refactor: Update UpdateActivityEventPresenter to handle activity stat…
FelipeCarillo May 21, 2024
f349ad6
refactor: Update UpdateActivityEventPresenter to handle activity stat…
FelipeCarillo May 21, 2024
028ce31
refactor: Update LambdaStack to include new function update_activity_…
FelipeCarillo May 21, 2024
4004c7b
refactor: Update UpdateActivityUsecase to validate and update status_…
FelipeCarillo May 21, 2024
7fa30d1
refactor: Update UpdateActivityPresenter to set status_activity to nu…
FelipeCarillo May 21, 2024
aef6116
refactor: Update populate_database.ts to create or update activity st…
FelipeCarillo May 23, 2024
ae27ad4
refactor: Update populate_database.ts to create or update activity st…
FelipeCarillo May 23, 2024
2565464
refactor: Update LambdaStack to include new function get_all_activiti…
FelipeCarillo May 24, 2024
c9b30ae
refactor: Update ActivityRepo to include get_all_activities_catalog m…
FelipeCarillo May 24, 2024
9a32897
refactor: Add GetAllActivitiesCatalog feature
FelipeCarillo May 24, 2024
b629854
refactor: Add unit test for GetAllActivitiesCatalog feature
FelipeCarillo May 24, 2024
75cf320
refactor: Update GetAllActivitiesCatalogController to handle error cases
FelipeCarillo May 24, 2024
8b96159
refactor: Include partner institutions in get_all_activities_catalog …
FelipeCarillo May 24, 2024
a2f61ed
refactor: Exclude unnecessary attributes in get_all_activities_catalo…
FelipeCarillo May 24, 2024
23113cd
refactor: Update ActivityRepo to include get_all_activities_catalog m…
FelipeCarillo May 24, 2024
630486a
refactor: Include partner institutions in get_all_activities_catalog …
FelipeCarillo May 24, 2024
e6c0ea8
refactor: Include partner institutions in get_all_activities_catalog …
FelipeCarillo May 24, 2024
0a2091d
refactor: Include partner institutions in get_all_activities_catalog …
FelipeCarillo May 24, 2024
eac58dc
refactor: Include partner institutions in get_all_activities_catalog …
FelipeCarillo May 24, 2024
35550d1
refactor: Update ActivityRepo to include get_all_activities_catalog m…
FelipeCarillo May 24, 2024
e92ab50
chore: Add validation for missing name parameter in CreateModeratorUs…
FelipeCarillo May 25, 2024
5a8988b
refactor: Add method to UserRepo to get all moderators
FelipeCarillo May 25, 2024
221e4fa
refactor: Add GetAllModerators feature
FelipeCarillo May 25, 2024
ad48eef
refactor: Add GetAllModerators feature
FelipeCarillo May 25, 2024
b0fdc88
feat: Add Deleted class to handle HTTP 204 responses
LucaPinheiro May 25, 2024
7558c61
feat: Add delete_moderator method to UserRepo
LucaPinheiro May 25, 2024
5b00daa
feat: Implement delete_moderator method in UserRepo
LucaPinheiro May 25, 2024
7734a01
Implement delete_moderator method in UserRepo
LucaPinheiro May 25, 2024
cf62ba2
feat: Implement DeleteModeratorController
LucaPinheiro May 25, 2024
2362f65
feat: Add delete_moderator_presenter module
LucaPinheiro May 25, 2024
fc73709
feat: Add DeleteModeratorUsecase
LucaPinheiro May 25, 2024
afe69c6
feat: Add unit tests for Delete Moderator Presenter
LucaPinheiro May 25, 2024
ff440c4
feat: Update DeleteModeratorController to handle ParameterError
LucaPinheiro May 25, 2024
b2c673e
needs 2 errors to verify
LucaPinheiro May 25, 2024
f0e0d21
feat: Add delete_moderator lambda function
LucaPinheiro May 25, 2024
dec9b6b
fix: Correct HTTP method for delete_moderator lambda function
LucaPinheiro May 25, 2024
0e7a3d4
refactor: Add validation for maua.br domain in CreateModeratorUsecase
FelipeCarillo May 26, 2024
1015eda
refactor: Update GetAllModeratorsController to use OK response code
FelipeCarillo May 26, 2024
62f0910
refactor: Update CreateModeratorUsecase to use async/await for updati…
FelipeCarillo May 26, 2024
ec1d98b
refactor: Remove unused Deleted class from http_codes.ts
LucaPinheiro May 27, 2024
c0b5726
refactor: Update DeleteModeratorController to use OK class for response
LucaPinheiro May 27, 2024
c460e6d
refactor: Remove commented out code in delete_moderator.test.ts
LucaPinheiro May 27, 2024
4f2d3a2
feat: Handle additional errors in DeleteModeratorController
LucaPinheiro May 27, 2024
9317cfc
refactor: Remove commented out code in delete_moderator.test.ts
LucaPinheiro May 27, 2024
efeac8b
refactor: Update DeleteModeratorController to handle missing request …
LucaPinheiro May 27, 2024
5aa71d3
refactor: Update DeleteModeratorUsecase to use request body instead o…
LucaPinheiro May 27, 2024
8000daf
refactor: Remove commented out code in delete_moderator.test.ts
LucaPinheiro May 27, 2024
c79b281
refactor: Update DeleteModeratorController to throw InvalidRequest in…
LucaPinheiro May 27, 2024
ab6e439
refactor: Update DeleteModeratorUsecase to throw InvalidRequest for m…
LucaPinheiro May 27, 2024
3d0b5fc
refactor: Update DeleteModeratorUsecase to throw InvalidRequest for m…
LucaPinheiro May 27, 2024
70de3a5
Merge pull request #38 from Instituto-Maua-de-Tecnologia/adding/delet…
LucaPinheiro May 27, 2024
2f0a779
refactor: Update event trigger names for activity start and end
FelipeCarillo May 29, 2024
c83cb54
Merge branch 'dev' of https://github.com/Instituto-Maua-de-Tecnologia…
FelipeCarillo May 29, 2024
7b4e308
refactor: Update event trigger names for activity start and end
FelipeCarillo May 29, 2024
ac96aca
refactor: Update event trigger names for activity start and end
FelipeCarillo May 29, 2024
c3c844b
refactor: Update event trigger names for activity start and end
FelipeCarillo May 29, 2024
70ab790
refactor: Update event trigger names for activity start and end
FelipeCarillo May 29, 2024
ac7f41f
refactor: Add EventBridge permission for lambda function
FelipeCarillo May 29, 2024
936adf0
refactor: Update update_users_activity_usecase to handle conflict errors
FelipeCarillo Jun 9, 2024
84bce6e
refactor: Update update_users_activity_status to use forEach instead …
FelipeCarillo Jun 9, 2024
7b8bb04
refactor: Update ActivityRepo and UpdateUsersActivityUsecase to toggl…
FelipeCarillo Jun 9, 2024
0191985
refactor: Simplify code for toggling user status in ActivityRepo
FelipeCarillo Jun 9, 2024
7016a1d
refactor: Add EventBridge permission for lambda function
FelipeCarillo Jun 10, 2024
ce59cdc
refactor: Update get_catalog module to use simplified code structure
FelipeCarillo Jul 19, 2024
1cca407
refactor: Remove unused test file for get_all_activities_catalog
FelipeCarillo Jul 19, 2024
62ed305
refactor: Add unit test for get_catalog module
FelipeCarillo Jul 19, 2024
2081bcc
refactor: Update get_all_activities_catalog to get_catalog
FelipeCarillo Jul 19, 2024
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
3 changes: 1 addition & 2 deletions iac/lib/iac_stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ export class IacStack extends cdk.Stack {
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowHeaders: ["*"],
}
}
);
});

const bucket = new Bucket(this, "Coil_Bucket", {
bucketName: "coil-bucket",
Expand Down
57 changes: 52 additions & 5 deletions iac/lib/lambda_stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@ export class LambdaStack extends Construct {
private get_user: lambda_js.NodejsFunction;
private auth_user: lambda_js.NodejsFunction;
private create_moderator: lambda_js.NodejsFunction;

private get_all_moderators: lambda_js.NodejsFunction;
private delete_moderator: lambda_js.NodejsFunction;

private get_institution: lambda_js.NodejsFunction;
private create_institution: lambda_js.NodejsFunction;
private update_institution: lambda_js.NodejsFunction;
private get_all_institutions: lambda_js.NodejsFunction;
private get_institution_requirements: lambda_js.NodejsFunction;

private assign_user: lambda_js.NodejsFunction;
private assign_user: lambda_js.NodejsFunction;
private get_activity: lambda_js.NodejsFunction;
private create_activity: lambda_js.NodejsFunction;
private update_activity: lambda_js.NodejsFunction;
private get_all_activities: lambda_js.NodejsFunction;
private update_users_activity: lambda_js.NodejsFunction;
private update_activity_event: lambda_js.NodejsFunction;
private get_activity_requirements: lambda_js.NodejsFunction;
private get_catalog: lambda_js.NodejsFunction;
private get_all_activities_enrolled: lambda_js.NodejsFunction;

public functions_need_s3_access: lambda.Function[] = [];
public function_need_event_bridge_access: lambda.Function[] = [];
public functions_need_event_bridge_access: lambda.Function[] = [];

private create_lambda(
Expand Down Expand Up @@ -57,9 +61,9 @@ export class LambdaStack extends Construct {

restapi_resource.addResource(function_name.replace(/_/g, "-"), {
defaultCorsPreflightOptions: {
allowOrigins: origins,
allowMethods: [method],
allowHeaders: ["*"],
allowOrigins: origins,
allowMethods: [method],
allowHeaders: ["*"],
}
}).addMethod(method, new apigw.LambdaIntegration(function_lambda));

Expand Down Expand Up @@ -104,6 +108,14 @@ export class LambdaStack extends Construct {
origins
);

this.delete_moderator = this.create_lambda(
"delete_moderator",
environment_variables,
"GET",
restapi_resource,
origins
);

this.create_activity = this.create_lambda(
"create_activity",
environment_variables,
Expand Down Expand Up @@ -222,6 +234,22 @@ export class LambdaStack extends Construct {
origins
)

this.get_catalog = this.create_lambda(
"get_catalog",
environment_variables,
"GET",
restapi_resource,
origins
)

this.get_all_moderators = this.create_lambda(
"get_all_moderators",
environment_variables,
"GET",
restapi_resource,
origins
)

this.functions_need_s3_access = [
this.create_institution,
this.update_institution,
Expand All @@ -230,6 +258,25 @@ export class LambdaStack extends Construct {
this.functions_need_event_bridge_access = [
this.create_activity,
this.update_activity,
this.update_activity_event,
]

this.functions_need_event_bridge_access.forEach((function_lambda) => {
function_lambda.addToRolePolicy(
new iam.PolicyStatement({
actions: [
"events:PutRule",
"events:PutTargets",
"events:RemoveTargets",
"events:DeleteRule",
],
resources: ["*"],
}))
function_lambda.addPermission("EventBridgePermission", {
principal: new iam.ServicePrincipal("events.amazonaws.com"),
sourceArn: "arn:aws:events:us-east-1:123456789012:rule/*",
action: "lambda:InvokeFunction",
});
});
}
}
44 changes: 41 additions & 3 deletions populate_database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,39 @@ async function createOrUpdateUser(user: UserEntity): Promise<void> {
}
}

async function createOrUpdateStatusActivity(status: ActivityStatusEnum) {
try {
let existing = await ActivityStatus.findOne({ where: { id: status } });
let name: string;
switch (status) {
case ActivityStatusEnum.ACTIVE:
name = "Apply Now";
break;
case ActivityStatusEnum.CANCELED:
name = "Canceled";
break;
case ActivityStatusEnum.ENDED:
name = "Ended";
break;
case ActivityStatusEnum.ON_HOLD:
name = "Under Analysis";
break;
case ActivityStatusEnum.TO_START:
name = "Coming Soon";
break;
}
if (!existing) {
await ActivityStatus.create({ name: name });
console.log(`Activity status ${status} created`);
} else {
await ActivityStatus.update({ name: name }, { where: { id: status } });
console.log(`Activity status ${status} updated`);
}
} catch (error) {
console.error(`Error creating or updating activity status ${status}:`, error);
}
}

(async () => {
try {
await handleDatabaseCreation();
Expand All @@ -278,12 +311,17 @@ async function createOrUpdateUser(user: UserEntity): Promise<void> {
await handleLanguagesCreation();

await handleCountriesCreation();

await handleCriteriasCreation();

await createOrUpdateSocialMedia();
await createOrUpdateSocialMedia();

await createOrUpdateStatusActivity(ActivityStatusEnum.ACTIVE);
await createOrUpdateStatusActivity(ActivityStatusEnum.CANCELED);
await createOrUpdateStatusActivity(ActivityStatusEnum.ENDED);
await createOrUpdateStatusActivity(ActivityStatusEnum.ON_HOLD);
await createOrUpdateStatusActivity(ActivityStatusEnum.TO_START);

await createOrUpdateEnumItems(ActivityStatus, activityStatuses, ActivityStatusEnum);
await createOrUpdateEnumItems(ActivityType, activityTypes, ActivityTypeEnum);
await createOrUpdateEnumItems(UserType, userTypes, UserTypeEnum);

Expand Down
42 changes: 18 additions & 24 deletions src/core/helpers/functions/event_bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ dotenv.config();


export class EventBridgeManager {
private event: EventBridge;
private lambda: Lambda;
private aws_region: string;
private aws_account_id: string;
private event: EventBridge;

constructor() {
this.event = new EventBridge();
this.lambda = new Lambda();
this.aws_region = process.env.AWS_REGION as string;
this.aws_account_id = process.env.AWS_ACCOUNT_ID as string;
this.event = new EventBridge();
}

private async get_rule(rule_name: string): Promise<any | undefined> {
Expand All @@ -40,20 +36,11 @@ export class EventBridgeManager {
}).promise();
const lambda_arn = lambda_response.Configuration?.FunctionArn as string;

// Add permission
await this.lambda.addPermission({
Action: "lambda:InvokeFunction",
FunctionName: lambda_function,
Principal: "events.amazonaws.com",
StatementId: rule_name,
SourceArn: "arn:aws:events:" + this.aws_region + ":" + this.aws_account_id + ":rule/" + rule_name,
}).promise();

// Create CloudWatch Events rule
await this.event.putRule({
Name: rule_name,
ScheduleExpression: "cron(" + date.getMinutes() + " " + date.getHours() + " " + date.getDate() + " " + (date.getMonth() + 1) + " ? " + date.getFullYear() + ")",
State: "ENABLED",
State: "ENABLED"
}).promise();

// Add target to the rule
Expand All @@ -72,27 +59,34 @@ export class EventBridgeManager {
},
],
}).promise();

// Add permission to Lambda function
await this.lambda.addPermission({
FunctionName: lambda_function,
StatementId: rule_name,
Action: "lambda:InvokeFunction",
Principal: "events.amazonaws.com",
SourceArn: "arn:aws:events:" + process.env.AWS_REGION + ":" + process.env.AWS_ACCOUNT_ID + ":rule/" + rule_name,
}).promise();

return true;
}

public async delete_trigger(rule_name: string, lambda_function: string): Promise<boolean> {
const has_rule = await this.get_rule(rule_name);
lambda_function = lambda_function + "_Coil";

if (has_rule) {
await this.lambda.removePermission({
FunctionName: lambda_function,
StatementId: rule_name,
}).promise();


await this.event.removeTargets({
Rule: rule_name,
Ids: [rule_name],
}).promise();

await this.event.deleteRule({
Name: rule_name,
}).promise();

return true;
} else {
return true;
Expand Down
96 changes: 86 additions & 10 deletions src/core/repositories/database/repositories/ActivityRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
Country,
SocialMedia
} from "../models/Models";
import { a } from "vitest/dist/suite-IbNSsUWN";


export class ActivityRepo implements IActivityRepo {
Expand Down Expand Up @@ -158,8 +159,6 @@ export class ActivityRepo implements IActivityRepo {
return null;
}

console.log(activities.map((activity) => activity.toJSON()));

return activities.map((activity) => this.ActivityDTO.to_entity(activity.toJSON()));
}

Expand Down Expand Up @@ -354,7 +353,7 @@ export class ActivityRepo implements IActivityRepo {
id: activity.id
}
});

await ActivityPartnerInstitution.destroy({
where: {
activity_id: activity.id
Expand Down Expand Up @@ -438,19 +437,96 @@ export class ActivityRepo implements IActivityRepo {
}

async update_users_activity_status(activity_id: string, users: { user_id: string, status: boolean }[]): Promise<boolean> {
for (let user of users) {
const response = await ActivityApplication.update({
status: user.status
let response = true;

users.forEach(async user => {
const response_user = await ActivityApplication.update({
status: !user.status
}, {
where: {
activity_id: activity_id,
user_id: user.user_id
}
});
if (response[0] === 0) {
return false;
if (response_user[0] === 0) {
response = false;
}
}
return true;
});

return response;
}

async get_all_activities_catalog(): Promise<{ title: string; logo: string; type_activity: ActivityTypeEnum; }[]> {
const response_project = await ActivityDB.findAll({
include: [{
model: ActivityPartnerInstitution, as: 'partner_institutions',
include: [{
model: Institution,
as: 'institution',
include: [{
model: InstitutionImageDB, as: 'images',
limit: 1,
order: [['id', 'ASC']]
}]
}]
}],
where: {
status_id: {
[Op.or]: [ActivityStatusEnum.ACTIVE, ActivityStatusEnum.TO_START]
},
type_id: ActivityTypeEnum.PROJECT
},
order: [
['start_date', 'ASC']
],
limit: 5
});

const response_mobility = await ActivityDB.findAll({
include: [{
model: ActivityPartnerInstitution, as: 'partner_institutions',
include: [{
model: Institution,
as: 'institution',
include: [{
model: InstitutionImageDB, as: 'images',
limit: 1,
order: [['id', 'ASC']]
}]
}]
}],
where: {
status_id: {
[Op.or]: [ActivityStatusEnum.ACTIVE, ActivityStatusEnum.TO_START]
},
type_id: ActivityTypeEnum.ACADEMIC_MOBILITY
},
order: [
['start_date', 'ASC']
],
limit: 5
});

let activities = response_project.concat(response_mobility);

let activities_json: {
title: string;
partner_institutions: {
institution: {
images: { image: string }[];
};
}[];
type_id: ActivityTypeEnum;
}[] = activities.map(activity => activity.toJSON());

activities_json = activities_json.map(activity => (
activity = activity.partner_institutions.length > 0 ? activity : { ...activity, partner_institutions: [{ institution: { images: [{ image: "" }] } }] }
));

return activities_json.map(activity => ({
title: activity.title,
logo: activity.partner_institutions[0].institution.images[0].image,
type_activity: activity.type_id
}));
}
}
Loading
Loading