Skip to content

Commit

Permalink
Revert "Merge pull request #23 from Instituto-Maua-de-Tecnologia/crea…
Browse files Browse the repository at this point in the history
…ting-get-activity"

This reverts commit 87da015, reversing
changes made to 770bc22.
  • Loading branch information
FelipeCarillo committed May 9, 2024
1 parent 87da015 commit 9c1b2b5
Show file tree
Hide file tree
Showing 78 changed files with 3,380 additions and 1,149 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SECRET_KEY=
AZURE_URL=
STAGE=
7 changes: 6 additions & 1 deletion .github/workflows/CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ jobs:
npm install -g aws-cdk
npm install -g ts-node
npm install
npm run populate-db
cd iac
npm install
- name: Populate DB
run: |
echo "Populating DB"
npm run populate-db
env:
STAGE: ${{ github.ref_name }}
RDS_HOSTNAME: ${{ secrets.RDS_HOSTNAME }}
RDS_USERNAME: ${{ secrets.RDS_USERNAME }}
RDS_PASSWORD: ${{ secrets.RDS_PASSWORD }}
Expand Down
19 changes: 14 additions & 5 deletions iac/lib/iac_stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RestApi } from 'aws-cdk-lib/aws-apigateway';
export class IacStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const restapi = new RestApi(
this, "Coil_Restapi", {
restApiName: "CoilRestApi",
Expand All @@ -18,13 +18,16 @@ export class IacStack extends cdk.Stack {
allowOrigins: ["*"],
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowHeaders: ["*"],
}}
}
}
);

const bucket = new Bucket(this, "Coil_Bucket", {
bucketName: "coil-bucket",
removalPolicy: cdk.RemovalPolicy.DESTROY,
publicReadAccess: true,
});
bucket.grantPublicAccess();

const coil_resource = restapi.root.addResource("coil", {
defaultCorsPreflightOptions: {
Expand All @@ -34,11 +37,13 @@ export class IacStack extends cdk.Stack {
}
});

const ENVIROMMENT_VARIABLES: {[key: string]: string} = {
const ENVIROMMENT_VARIABLES: { [key: string]: string } = {
"AWS_ACCOUNT_ID": process.env.AWS_ACCOUNT_ID || "",
"DOMAIN": process.env.DOMAIN || "",
"STAGE": process.env.STAGE || "test",
"AZURE_URL": process.env.AZURE_URL || "",
"SECRET_KEY": process.env.SECRET_KEY || "",
"AWS_BUCKET": bucket.bucketName,
"RDS_HOSTNAME": process.env.RDS_HOSTNAME || "",
"RDS_PORT": process.env.RDS_PORT || "",
"RDS_DB_NAME": process.env.RDS_DB_NAME || "",
Expand All @@ -54,13 +59,17 @@ export class IacStack extends cdk.Stack {
coil_resource
);

for (const lambda_function of lambda_stack.functions_need_event_bridge_access) {
lambda_stack.functions_need_event_bridge_access.forEach((lambda_function: cdk.aws_lambda.Function) => {
lambda_function.addToRolePolicy(
new iam.PolicyStatement({
actions: ["events:*", "lambda:*"],
resources: ["*"],
})
);
}
});

lambda_stack.functions_need_s3_access.forEach((lambda_function: cdk.aws_lambda.Function) => {
bucket.grantReadWrite(lambda_function);
});
}
}
153 changes: 143 additions & 10 deletions iac/lib/lambda_stack.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
import { Construct } from "constructs";
import { aws_lambda as lambda, aws_lambda_nodejs as lambda_js, aws_apigateway as apigw, aws_iam as iam, Duration} from "aws-cdk-lib";
import { aws_lambda as lambda, aws_lambda_nodejs as lambda_js, aws_apigateway as apigw, aws_iam as iam, Duration } from "aws-cdk-lib";

export class LambdaStack extends Construct {

private get_user: lambda_js.NodejsFunction;
private auth_user: lambda_js.NodejsFunction;
private update_user: lambda_js.NodejsFunction;
private create_moderator: lambda_js.NodejsFunction;

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

private assign_user: lambda_js.NodejsFunction;
private create_activity: lambda_js.NodejsFunction;
private update_activity: lambda_js.NodejsFunction;
private get_all_activities: lambda_js.NodejsFunction;
private update_user_activity: lambda_js.NodejsFunction;
private update_activity_event: lambda_js.NodejsFunction;
private get_all_activities_enrolled: lambda_js.NodejsFunction;

private get_all_courses: lambda_js.NodejsFunction;

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


private create_lambda(
function_name: string,
environment_variables: {[key: string]: string},
environment_variables: { [key: string]: string },
method: string,
restapi_resource: apigw.Resource,
origins: string[] = apigw.Cors.ALL_ORIGINS,
) {

function toTittle(string:string) {
function toTittle(string: string) {
return string.toLowerCase().split("_").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("_");
}

Expand All @@ -38,11 +54,11 @@ export class LambdaStack extends Construct {
}
);

restapi_resource.addResource(function_name.replace("_", "-"),{
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 All @@ -53,9 +69,9 @@ export class LambdaStack extends Construct {
constructor(
scope: Construct,
id: string,
environment_variables: {[key: string]: string },
environment_variables: { [key: string]: string },
restapi_resource: apigw.Resource
) {
) {
super(scope, id);

let origins = ["*"];
Expand All @@ -79,7 +95,124 @@ export class LambdaStack extends Construct {
origins
);

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

this.update_user = this.create_lambda(
"update_user",
environment_variables,
"POST",
restapi_resource,
origins
);

this.create_activity = this.create_lambda(
"create_activity",
environment_variables,
"POST",
restapi_resource,
origins
);

this.update_activity = this.create_lambda(
"update_activity",
environment_variables,
"POST",
restapi_resource,
origins
);

this.update_activity_event = new lambda_js.NodejsFunction(
this,
"Update_Activity_Event_Coil",
{
functionName: "Update_Activity_Event_Coil",
entry: `../src/modules/update_activity_event/app/update_activity_event_presenter.ts`,
handler: `handler`,
environment: environment_variables,
runtime: lambda.Runtime.NODEJS_20_X,
timeout: Duration.seconds(15),
memorySize: 256
}
);

this.create_institution = this.create_lambda(
"create_institution",
environment_variables,
"POST",
restapi_resource,
origins
);

this.update_institution = this.create_lambda(
"update_institution",
environment_variables,
"POST",
restapi_resource,
origins
);

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

this.update_user_activity = this.create_lambda(
"update_user_activity",
environment_variables,
"POST",
restapi_resource,
origins
);

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

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

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

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

this.functions_need_s3_access = [
this.create_institution,
this.update_institution,
]

this.functions_need_event_bridge_access = [
this.create_activity,
this.update_activity,
]
}
}
55 changes: 35 additions & 20 deletions populate_database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dotenv from 'dotenv';
import { randomUUID } from 'crypto';

import {
User, Course, UserType, ActivityCourse, Institution, InstitutionImage, InstitutionSocialMedia,
ActivityStatus, ActivityType, Activity, ActivityApplication, ActivityLanguage, ActivityCriteria, ActivityPartnerInstitution,
Expand All @@ -16,6 +17,15 @@ const courses: string[] = [
"Engenharia de Alimentos", "Engenharia Civil"
];

const institutions = [
{
id: randomUUID(),
name: "Fontys University of Applied Sciences",
country: "Netherlands",
email: "teste@test.com"
}
];

const userTypes: UserTypeEnum[] = [
UserTypeEnum.ADMIN, UserTypeEnum.STUDENT, UserTypeEnum.MODERATOR
];
Expand All @@ -29,31 +39,21 @@ const activityTypes: ActivityTypeEnum[] = [
];

async function handleDatabaseCreation(): Promise<void> {
await Promise.all([
UserType,
Course,
Institution,
User,
ActivityStatus,
ActivityType,
InstitutionImage,
InstitutionSocialMedia,
Activity,
ActivityApplication,
ActivityLanguage,
ActivityCriteria,
ActivityPartnerInstitution,
ActivityCourse
].map(model => model.sync({ alter: true })));
const stage = process.env.STAGE || "";
const models = [UserType, Course, Institution, User, ActivityStatus, ActivityType, InstitutionImage, InstitutionSocialMedia, Activity, ActivityApplication, ActivityLanguage, ActivityCriteria, ActivityPartnerInstitution, ActivityCourse];

for (const model of models) {
await model.sync({ alter: true });
}
}

async function createOrUpdateUser(name: string, email: string, userType: UserTypeEnum, courseId: number | null, semester: number | null): Promise<void> {
async function createOrUpdateUser(id: string, name: string, email: string, userType: UserTypeEnum, courseId: number | null, semester: number | null): Promise<void> {
let user = await User.findOne({ where: { email } });
if (user) {
await user.update({ userTypeId: userType, courseId, semester });
} else {
await User.create({
id: randomUUID(),
id: id,
name,
email,
user_type_id: userType,
Expand All @@ -66,6 +66,15 @@ async function createOrUpdateUser(name: string, email: string, userType: UserTyp
console.log(`User ${name} ${user ? 'updated' : 'created'}`);
}

async function createOrUpdateInstitution(institution: any): Promise<void> {
let existingInstitution = await Institution.findOne({ where: { name: institution.name } });
if (!existingInstitution) {
await Institution.create(institution);
console.log(`Institution ${institution.name} created`);
}
console.log(`Institutions checked/created`);
}

async function createOrUpdateEnumItems(model: any, enumItems: number[], enumType: any): Promise<void> {
for (const enumItem of enumItems) {
let item = await model.findOne({ where: { id: enumItem } });
Expand Down Expand Up @@ -93,15 +102,21 @@ async function handleCoursesCreation(): Promise<void> {
await handleDatabaseCreation();
console.log("Database created");
await handleCoursesCreation();
console.log("Courses checked/created");
await createOrUpdateEnumItems(UserType, userTypes, UserTypeEnum);
await createOrUpdateEnumItems(ActivityStatus, activityStatuses, ActivityStatusEnum);
await createOrUpdateEnumItems(ActivityType, activityTypes, ActivityTypeEnum);
await createOrUpdateUser("Relações Internacionais", "relacoes-internacionais@maua.br", UserTypeEnum.ADMIN, 1, null);
console.log("Enums checked/created");
await createOrUpdateUser(randomUUID(), "Relações Internacionais", "relacoes-internacionais@maua.br", UserTypeEnum.ADMIN, 1, null);
const stage = process.env.STAGE || "";
if (["dev", "test"].includes(stage)) {
await createOrUpdateUser("Felipe Carillo", "23.00765-6@maua.br", UserTypeEnum.ADMIN, 1, 1);
await createOrUpdateUser("beba67f0-c5f2-4c18-9d30-2fa262763e62", "Felipe Carillo", "23.00765-6@maua.br", UserTypeEnum.ADMIN, null, null);
await createOrUpdateUser("d34c5cef-e295-40a6-b9a0-26eabdcc6d91", "Master Chief", "84560320168@maua.br", UserTypeEnum.MODERATOR, null, null);
await createOrUpdateUser("ae706466-c2e2-412b-8da1-230cb752f925", "Alejandro", "10000006@maua.br", UserTypeEnum.STUDENT, 1, 1);
}
console.log("Users checked/created");
await createOrUpdateInstitution(institutions[0]);
console.log("Institutions checked/created");
} catch (error) {
console.error(error);
}
Expand Down
Loading

0 comments on commit 9c1b2b5

Please sign in to comment.