Skip to content

Commit

Permalink
Merge pull request #4 from icefoganalytics/main
Browse files Browse the repository at this point in the history
Centres
  • Loading branch information
datajohnson authored Feb 20, 2023
2 parents 504b805 + b9a6106 commit 51a7440
Show file tree
Hide file tree
Showing 36 changed files with 832 additions and 503 deletions.
16 changes: 1 addition & 15 deletions src/api/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ switch (process.env.NODE_ENV) {

dotenv.config({ path: path });

// Filter out the variables that the frontend needs to know about
let obj = process.env;
let pattern = "VUE_APP_";

export const VUE_APP: any = Object.keys(obj)
.filter((k) => k.includes(pattern))
.reduce((cur, key) => {
return Object.assign(cur, { [key]: obj[key] });
}, {});

console.log(`LOADING ${NODE_ENV} CONFIG FROM ${path}`);

export const apiBaseUrl = process.env.NODE_ENV == "production" ? "" : "http://localhost:3000";
Expand All @@ -37,11 +27,7 @@ export const AUTH0_AUDIENCE = process.env.VUE_APP_AUTH_AUDIENCE || "";

export const APPLICATION_NAME = process.env.APPLICATION_NAME || "";

export const MONGO_DB = process.env.MONGO_DB || "";
export const MONGO_HOST = process.env.MONGO_HOST || "";
export const MONGO_URL = `mongodb://${process.env.MONGO_USERNAME}:${process.env.MONGO_PASSWORD}@${process.env.MONGO_HOST}`;

export const MAIL_FROM = process.env.MAIL_FROM || "signing-authority@yukon.ca";
export const MAIL_FROM = process.env.MAIL_FROM || "visitor-analytics@yukon.ca";
export const MAIL_HOST = process.env.MAIL_HOST || "smtp.gov.yk.ca";
export const MAIL_PORT = process.env.MAIL_PORT || 25;
export const MAIL_USER = process.env.MAIL_USER || "";
Expand Down
1 change: 1 addition & 0 deletions src/api/src/data/migrations/001_create-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ exports.up = function (knex: knex.Knex, Promise: any) {
t.string("directory_id", 100).nullable();
t.dateTime("create_date").notNullable();
t.string("roles", 1000).nullable();
t.boolean("is_admin").notNullable().defaultTo(false);
});
};

Expand Down
19 changes: 19 additions & 0 deletions src/api/src/data/migrations/002_create-visitor_centre.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as knex from "knex";

exports.up = function (knex: knex.Knex, Promise: any) {
console.log("Created visitor_centre table");

return knex.schema.createTable("visitor_centre", function (t) {
t.increments("id").primary();
t.string("name", 200).notNullable().unique();
t.string("community", 200).notNullable();
t.string("region", 200).notNullable();
t.boolean("is_active").notNullable().defaultTo(true);
});
};

exports.down = function (knex: knex.Knex, Promise: any) {
console.log("Dropped visitor_centre table");

return knex.schema.dropTable("visitor_centre");
};
21 changes: 21 additions & 0 deletions src/api/src/data/migrations/003_create-centre_season.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as knex from "knex";

exports.up = function (knex: knex.Knex, Promise: any) {
console.log("Created visitor_centre_season table");

return knex.schema.createTable("visitor_centre_season", function (t) {
t.increments("id").primary();
t.integer("visitor_centre_id").notNullable().references("visitor_centre.id");
t.string("year", 4).notNullable();
t.date("open_date").notNullable();
t.date("close_date").notNullable();
t.integer("projected_visitors").notNullable().defaultTo(-1);
t.string("notes", 2000).nullable();
});
};

exports.down = function (knex: knex.Knex, Promise: any) {
console.log("Dropped visitor_centre_season table");

return knex.schema.dropTable("visitor_centre_season");
};
26 changes: 14 additions & 12 deletions src/api/src/data/migrator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Express, Request, Response } from "express";
import { NODE_ENV } from "../config";
import { sqldb } from "./index";
import { join } from "path";

Expand All @@ -18,18 +19,6 @@ export async function migrateLatest() {
}

export async function CreateMigrationRoutes(app: Express) {
// this initializes the migration tables if they don't already exist
await sqldb.migrate.list({ directory: join(__dirname, "migrations") });
const level = await sqldb.migrate.status({ directory: join(__dirname, "migrations") });

if (level < 0) {
console.log("-------- Migrations are behind - I'm going run them for you ---------");
await migrateLatest();
}
else {
console.log("-------- Migrations are up to date ---------");
}

app.get("/migrate/up", async (req: Request, res: Response) => {
res.send(await migrateUp());
});
Expand All @@ -41,4 +30,17 @@ export async function CreateMigrationRoutes(app: Express) {
app.get("/migrate/latest", async (req: Request, res: Response) => {
res.send(await migrateLatest());
});

if (NODE_ENV != "development") {
// this initializes the migration tables if they don't already exist
await sqldb.migrate.list({ directory: join(__dirname, "migrations") });
const level = await sqldb.migrate.status({ directory: join(__dirname, "migrations") });

if (level < 0) {
console.log("-------- Migrations are behind - I'm going run them for you ---------");
await migrateLatest();
} else {
console.log("-------- Migrations are up to date ---------");
}
}
}
3 changes: 2 additions & 1 deletion src/api/src/data/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ export interface User {
status: string;
ynet_id: string;
directory_id: string;
is_admin: Boolean;
create_date: Date;

department_admin_for?: string[];
roles?: string | string[];
scopes?: string[];
department_admin_for?: string | string[];
display_name?: string;
}
12 changes: 12 additions & 0 deletions src/api/src/data/models/visitor-centre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@ export interface VisitorCentre {
id: string;
name: string;
community: string;
region: string;
is_active: Boolean;
}

export interface VisitorCentreSeason {
id: number;
visitor_centre_id: number;
year: string;
open_date: Date;
close_date: Date;
projected_visitors: number;
notes: string;
}
5 changes: 2 additions & 3 deletions src/api/src/data/storage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { sqldb } from ".";
import { GenericService, UserService } from "../services";
import { UserService } from "../services";

export class Storage {
isInitialized: boolean = false;

UserStore!: UserService;

constructor() {
this.UserStore = new UserService(sqldb);
this.UserStore = new UserService();
}

async ensureConnected(): Promise<string> {
Expand Down
4 changes: 2 additions & 2 deletions src/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import helmet from "helmet";
import fileUpload from "express-fileupload";
import { API_PORT, FRONTEND_URL, APPLICATION_NAME } from "./config";
import { doHealthCheck } from "./utils/health-check";
import { userRouter, configRouter, permissionRouter } from "./routes";
import { userRouter, permissionRouter, visitorCentreRouter } from "./routes";
import { CreateMigrationRoutes } from "./data";
import { RequiresData } from "./middleware";
import axios from "axios";
Expand Down Expand Up @@ -52,7 +52,7 @@ app.get("/api/healthCheck", (req: Request, res: Response) => {
doHealthCheck(req, res);
});

app.use("/api/config", configRouter);
app.use("/api/visitor-centre", visitorCentreRouter);
app.use("/api/user", userRouter);
app.use("/api/permission", permissionRouter);

Expand Down
4 changes: 2 additions & 2 deletions src/api/src/middleware/authz.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import jwksRsa from "jwks-rsa";
import { AUTH0_DOMAIN, AUTH0_AUDIENCE } from "../config";
import { DirectoryService, UserService } from "../services";

import { sqldb } from "../data";
const db = new UserService(sqldb);
const db = new UserService();

export const checkJwt = jwt({
secret: jwksRsa.expressJwtSecret({
Expand Down Expand Up @@ -77,6 +76,7 @@ export async function loadUser(req: Request, res: Response, next: NextFunction)
last_name: resp.data.family_name,
ynet_id,
directory_id,
is_admin: false,
create_date: new Date(),
};

Expand Down
18 changes: 0 additions & 18 deletions src/api/src/routes/config-router.ts

This file was deleted.

7 changes: 1 addition & 6 deletions src/api/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import * as knex from "knex";
import { DB_CONFIG } from "../config";

export * from "./user-router";
export * from "./config-router";
export * from "./permission-router";

export const sqldb = knex.knex(DB_CONFIG);
export * from "./visitor-centre-router";
37 changes: 13 additions & 24 deletions src/api/src/routes/permission-router.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import express, { Request, Response } from "express";
import { param } from "express-validator";
import _ from "lodash";
import { RequiresData, ReturnValidationErrors } from "../middleware";
import { PermissionService } from "../services";
import { sqldb } from "../data";

import _ from "lodash";

export const permissionRouter = express.Router();
permissionRouter.use(RequiresData);

const permissionService = new PermissionService(sqldb);
const permissionService = new PermissionService();

permissionRouter.get("/check", async (req: Request, res: Response) => {
await permissionService
.check(req.body.email, req.body.scope)
.then((value) => {
res.status(200).send(value);
});
await permissionService.check(req.body.email, req.body.scope).then((value) => {
res.status(200).send(value);
});
});

permissionRouter.get("/getPermissions", async (req: Request, res: Response) => {
Expand All @@ -26,23 +22,16 @@ permissionRouter.get("/getPermissions", async (req: Request, res: Response) => {
});

permissionRouter.get("/addPermissions", async (req: Request, res: Response) => {
await permissionService
.add(req.body.email, req.body.operation)
.then((value) => {
res.status(200).send(value);
});
await permissionService.add(req.body.email, req.body.operation).then((value) => {
res.status(200).send(value);
});
});

permissionRouter.get(
"/removePermissions",
async (req: Request, res: Response) => {
await permissionService
.remove(req.body.email, req.body.scope)
.then((value) => {
res.status(200).send(value);
});
}
);
permissionRouter.get("/removePermissions", async (req: Request, res: Response) => {
await permissionService.remove(req.body.email, req.body.scope).then((value) => {
res.status(200).send(value);
});
});

permissionRouter.get("/test", async (req: Request, res: Response) => {
// await permissionService.add("maxrparker@gmail.com", [
Expand Down
Loading

0 comments on commit 51a7440

Please sign in to comment.