Skip to content

Commit

Permalink
[#175] - Reemplaza requires por import
Browse files Browse the repository at this point in the history
  • Loading branch information
rolivencia committed Oct 12, 2023
1 parent 3c6bb11 commit 7d81994
Show file tree
Hide file tree
Showing 16 changed files with 52 additions and 87 deletions.
1 change: 0 additions & 1 deletion api/_helpers/error-handler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = errorHandler;

function errorHandler(err, req, res, next) {
if (typeof err === "string") {
// custom application error
Expand Down
17 changes: 0 additions & 17 deletions api/_helpers/firebase-connector.js

This file was deleted.

10 changes: 5 additions & 5 deletions api/_helpers/jwt.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const expressJwt = require("express-jwt");
const environment = require("./environment");

module.exports = jwt;
import { environment } from "./environment";
import { expressjwt } from "express-jwt";

function jwt() {
const { secret } = environment.serverConfig;
return expressJwt({ secret: secret, algorithms: ["HS256"] }).unless({
return expressjwt({ secret: secret, algorithms: ["HS256"] }).unless({
path: [
// public routes that don't require authentication
"/users/authenticate",
Expand All @@ -15,3 +13,5 @@ function jwt() {
],
});
}

module.exports = jwt;
24 changes: 4 additions & 20 deletions api/_helpers/mysql-connector.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
const environment = require("./environment");
const Sequelize = require("sequelize");
import { environment } from "./environment";
import { Sequelize } from "sequelize";

const mysql = require("mysql2");
const ceutDatabase = environment.serverConfig.databases.mysql.databases
.filter((db) => db.id === "ceut-frsf")
.pop();

const mySqlConnection = mysql.createConnection({
host: ceutDatabase.host,
user: ceutDatabase.user,
database: ceutDatabase.database,
password: ceutDatabase.password,
});

const sequelizeConnection = new Sequelize(
const sequelizeConnector = new Sequelize(
ceutDatabase.database,
ceutDatabase.user,
ceutDatabase.password,
Expand All @@ -27,12 +19,4 @@ const sequelizeConnection = new Sequelize(
}
);

module.exports = { mysqlConnector, sequelizeConnector };

function mysqlConnector() {
return mySqlConnection;
}

function sequelizeConnector() {
return sequelizeConnection;
}
module.exports = { sequelizeConnector };
6 changes: 3 additions & 3 deletions api/_helpers/sanity-connector.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const environment = require("./environment");
const sanityClient = require("@sanity/client");
import { environment } from "./environment";
import { createClient } from "@sanity/client";

const client = sanityClient({
const client = createClient({
projectId: environment.sanity.projectId,
dataset: environment.sanity.dataset,
apiVersion: "2019-01-29", // use current UTC date - see "specifying API version"!
Expand Down
9 changes: 4 additions & 5 deletions api/_models/user/role.model.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const Sequelize = require("sequelize");
const connector = require("../../_helpers/mysql-connector");
const sequelizeConnector = connector.sequelizeConnector();

module.exports = () => Role;
import { sequelizeConnector } from "../../_helpers/mysql-connector";
import { Sequelize } from "sequelize";

class Role extends Sequelize.Model {}

Expand Down Expand Up @@ -45,3 +42,5 @@ Role.init(
modelName: "role",
}
);

module.exports = { Role };
9 changes: 4 additions & 5 deletions api/_models/user/user-role.model.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const Sequelize = require("sequelize");
const connector = require("../../_helpers/mysql-connector");
const sequelizeConnector = connector.sequelizeConnector();

module.exports = () => UserRole;
import { Sequelize } from "sequelize";
import { sequelizeConnector } from "../../_helpers/mysql-connector";

class UserRole extends Sequelize.Model {}

Expand Down Expand Up @@ -54,3 +51,5 @@ UserRole.init(
modelName: "user_role",
}
);

module.exports = { UserRole };
20 changes: 9 additions & 11 deletions api/_models/user/user.model.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
const Sequelize = require("sequelize");
const connector = require("../../_helpers/mysql-connector");
const sequelizeConnector = connector.sequelizeConnector();

const Role = require("./role.model");
const userRole = require("./user-role.model");
import { Sequelize } from "sequelize";
import { sequelizeConnector } from "../../_helpers/mysql-connector";
import Role from "./role.model";
import UserRole from "./user-role.model";

class User extends Sequelize.Model {}

module.exports = () => User;

User.init(
{
id: {
Expand Down Expand Up @@ -71,12 +67,14 @@ User.init(
);

// Mediante las dos llamadas siguientes, se define en Sequelize la relación N a M entre Role y User
User.belongsToMany(Role(), {
through: userRole(),
User.belongsToMany(Role, {
through: UserRole,
foreignKey: "id_user",
});

Role().belongsToMany(User, {
through: userRole(),
through: UserRole,
foreignKey: "id_role",
});

module.exports = { User };
2 changes: 1 addition & 1 deletion api/cards/[id].js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const sanityConnector = require("../_helpers/sanity-connector");
import sanityConnector from "../_helpers/sanity-connector";

export default async function getById(req, res) {
const { id } = req.query;
Expand Down
3 changes: 1 addition & 2 deletions api/cards/active.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";

const sanityConnector = require("../_helpers/sanity-connector");

import { sanityConnector } from "../_helpers/sanity-connector";
export default async function get(req, res) {
const cards = await sanityConnector.client.fetch(`*`, {});
const fixedCardIds = cards.filter((card) => card._type === "fixedCards").pop()
Expand Down
3 changes: 2 additions & 1 deletion api/cards/fixed.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

const sanityConnector = require("../_helpers/sanity-connector");
//TODO: Put this endpoint to work and retrieve the fixed cards.
import sanityConnector from "../_helpers/sanity-connector";

export default async function getFixed(req, res) {
const query = `*[_type == 'fixedCards']`;
const cards = await sanityConnector.client.fetch(query, {});
Expand Down
5 changes: 3 additions & 2 deletions api/courses/elective.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const environment = require("../_helpers/environment");
import { environment } from "../_helpers/environment";
import airtable from "airtable";

const cuenta = environment.serverConfig.airtable.cuentas
.filter((Cuenta) => Cuenta.id === "metas")
.pop();

//Se configura el objeto Airtable para hacer las consultas y obtener datos con los métodos
const airtable = require("airtable");
airtable.configure({
endpointUrl: environment.serverConfig.airtable.endpointUrl,
apiKey: cuenta.key,
Expand Down
8 changes: 4 additions & 4 deletions api/electoral-register/get.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const environment = require("../_helpers/environment");
const { google } = require("googleapis");
import { environment } from "../_helpers/environment";
import { googleapis } from "googleapis";

const apiKey = environment.google.apiKey;
const clientId = environment.google.clientId;
Expand All @@ -11,7 +11,7 @@ export default async function get(req, res) {

async function obtenerPadronDesdeGoogleSheets() {
return new Promise((resolve, reject) => {
const sheets = google.sheets({ version: "v4", auth: apiKey });
const sheets = googleapis.sheets({ version: "v4", auth: apiKey });

sheets.spreadsheets.values.get(
{
Expand All @@ -26,7 +26,7 @@ async function obtenerPadronDesdeGoogleSheets() {
legajo: row[0],
dni: row[1],
name: row[2],
carrera: row[3]
carrera: row[3],
};
});
resolve(result);
Expand Down
5 changes: 3 additions & 2 deletions api/sports/get.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const environment = require("../_helpers/environment");
import airtable from "airtable";
import { environment } from "../_helpers/environment";

const cuenta = environment.serverConfig.airtable.cuentas
.filter((Cuenta) => Cuenta.id === "ceut")
.pop();

//Se configura el objeto Airtable para hacer las consultas y obtener datos con los métodos
const airtable = require("airtable");
airtable.configure({
endpointUrl: environment.serverConfig.airtable.endpointUrl,
apiKey: cuenta.key,
Expand Down
15 changes: 8 additions & 7 deletions api/users/authenticate.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const User = require("../_models/user/user.model");
const Role = require("../_models/user/role.model");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const environment = require("../_helpers/environment");
import User from "../_models/user/user.model";
import Role from "../_models/user/role.model";
import bcrypt from "bcrypt";
import { environment } from "../_helpers/environment";

import { jsonwebtoken as jwt } from "jsonwebtoken";

export default async function authenticate(req, res) {
const { userName, password } = req.body;

const user = await User().findOne({
const user = await User.findOne({
raw: true,
include: [
{
model: Role(),
model: Role,
required: true,
attributes: ["id", "description"],
through: { attributes: [] },
Expand Down
2 changes: 1 addition & 1 deletion api/users/get.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const User = require("../_models/user/user.model");
import { User } from "../../src/app/_models";

export default async function get(req, res) {
const users = await User().findAll();
Expand Down

0 comments on commit 7d81994

Please sign in to comment.