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

Feature/data view #32

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions courses/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
db/*
13 changes: 13 additions & 0 deletions courses/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Courses

Run jobs for runners and then change data

1. start web
a. cd web
b. yarn install
c. yarn start

2. start the server
node index.js

# Thats it :D.
138 changes: 138 additions & 0 deletions courses/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// require('dotenv').config()
// var cors = require('cors');

import * as dotenv from 'dotenv';
dotenv.config();

import cors from 'cors';
import { initiateDb } from './utils/db.js';
initiateDb();

import express from 'express';
import { EngineService, MappingService, DataViewQuery, UserViewQuery } from './services/index.js';
import { Exports } from './models/index.js';

// const { initiateDb } = require('./utils/db');
// const express = require('express');
const app = express()
const port = process.env.PORT || 4000;
// const { EngineService } = require('./services')

app.use(cors())
app.use(express.json());

app.get('/', (req, res) => {
const rulesModel = require('./models/rule.model');
console.log(req.body);

let r = rulesModel.all();

res.json({
courses: []
})
})

app.get('/runners', (req, res) => {
console.log(req.body);

res.json({
runners: [
{
value: 'generateCourseShells',
name: 'Generate Course Shells'
},
{
value: 'generateEnrollment',
name: 'Generate Enrolment'
}
]
})
})

app.get('/exports', (req, res) => {
console.log(req.query);

if (req.query.jobId) {
Exports.FindByJobId(req.query.jobId, (err, data) => {
if (err) {
res.status(400).send('Error occured');
}

res.json({
exports: data
})
})
}
})

app.post('/mappings', (req, res) => {
console.log(req.body)
MappingService.createMappings(req.body.mapping, (err, mapping) => {
res.json({
mapping
})
})
})

app.get('/mappings', (req, res) => {
MappingService.getMappings((err, mappings) => {
res.json({
mappings
})
})
})

app.post('/runners', (req, res) => {
const run = EngineService.run(req.body.runner, res).then(() => {
console.log('done');
});

// res.json({
// done: true
// })
})

app.get('/custom_runners', (req, res) => {
console.log(req.body);

res.json({
runners: [
{
value: 'addCanvasCourses',
name: 'All Canvas Courses'
},
{
value: 'addJenActiveCourses',
name: 'Add Jenzabar Active Courses'
},
{
value: 'generateCourse',
name: 'Generate Course Shells'
}
]
})
})

app.post('/filter_courses', async (req, res) => {
let data = await DataViewQuery(req);

res.json({
data
});
})

app.post('/filter_users', async (req, res) => {
let data = await UserViewQuery(req);

res.json({
data
});
})

// Start a job



app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
});
9 changes: 9 additions & 0 deletions courses/migrations/create-courses.migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default (db) => {
db.run(`CREATE TABLE IF NOT EXISTS courses(
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
sisId TEXT NOT NULL,
terms TEXT NOT NULL,
attributes TEXT NOT NULL
);`)
}
11 changes: 11 additions & 0 deletions courses/migrations/create-exports.migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default (db) => {
db.run(`CREATE TABLE IF NOT EXISTS exports(
id INTEGER NOT NULL PRIMARY KEY,
data TEXT NOT NULL,
JobId TEXT NOT NULL
);`)

// db.run('DROP TABLE exports')
}

// data will be of type json although stringified as we might have a complex
12 changes: 12 additions & 0 deletions courses/migrations/create-jobs.migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default (db) => {
db.run(`CREATE TABLE IF NOT EXISTS jobs(
id INTEGER NOT NULL PRIMARY KEY,
category TEXT NOT NULL,
status TEXT NOT NULL,
started TEXT NOT NULL,
finished TEXT,
jobId TEXT NOT NULL
);`)

// db.run('DROP TABLE jobs')
}
12 changes: 12 additions & 0 deletions courses/migrations/create-mappings.migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default (db) => {
db.run(`CREATE TABLE IF NOT EXISTS mappings(
id INTEGER NOT NULL PRIMARY KEY,
source_key TEXT NOT NULL,
source_value TEXT NOT NULL,
map_key TEXT NOT NULL,
map_value TEXT NOT NULL,
uuid TEXT NOT NULL
);`)

// db.run('DROP TABLE mappings')
}
7 changes: 7 additions & 0 deletions courses/migrations/create-rules.migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default (db) => {
db.run(`CREATE TABLE IF NOT EXISTS rules(
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
rules TEXT NOT NULL
);`)
}
19 changes: 19 additions & 0 deletions courses/migrations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import rulesMigration from './create-rules.migration.js';
import courseMigration from './create-courses.migration.js';
import jobsMigration from './create-jobs.migration.js'
import createExportsMigration from './create-exports.migration.js';
import createMappingsMigration from './create-mappings.migration.js';

export default (db) => {
rulesMigration(db);
courseMigration(db);
jobsMigration(db);
createExportsMigration(db);
createMappingsMigration(db);
}

// module.exports = (db) => {

// // require('./create-rules.migration')(db);
// // require('./create-courses.migration');
// }
15 changes: 15 additions & 0 deletions courses/models/course.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// const { db } = require('../utils/db');
import { db } from '../utils/db';

// DB model for courses
module.exports = {
table: 'courses',
connection() { return db.connection },
all() { return this.connection().run(`Select * from ${this.table}`) },
attributesParser(data) { return JSON.stringify(data.attributes || {})},
create(data) {
return this.connection()
.run(`Insert into ${this.table} (name, sisId, terms, attributes)
VALUES (${data.name, data.rules, data.terms, this.attributesParser(data)})`)
}
}
19 changes: 19 additions & 0 deletions courses/models/exports.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { db } from '../utils/db.js';

const TABLE = 'exports';
function connection() {
return db.connection;
}

export function All(callback) {
return connection().all(`Select * from ${TABLE}`, [], callback);
}

export function Create(data, callback) {
return connection().run(`Insert into ${TABLE} (data, jobId)
VALUES (?, "${data.jobId}");`, [data.stringifiedData], callback)
}

export function FindByJobId(jobId ,callback) {
return connection().all(`Select * from ${TABLE} WHERE jobId = ?`, [jobId], callback)
}
3 changes: 3 additions & 0 deletions courses/models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * as Job from './job.model.js';
export * as Exports from './exports.model.js';
export * as Mapping from './mapping.model.js';
27 changes: 27 additions & 0 deletions courses/models/job.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { db } from '../utils/db.js';

// DB model for courses
// export default {
// table: 'jobs',
// connection() { return db.connection },
// all() { return this.connection().run(`Select * from ${this.table}`) },
// attributesParser(data) { return JSON.stringify(data.attributes || {})},
// create(data) {

// }
// }

const TABLE = 'jobs';

function connection() {
return db.connection;
}

export function All(callback) {
return connection().all(`Select * from ${TABLE}`, [], callback);
}

export function Create(data, callback) {
return connection().run(`Insert into ${TABLE} (category, status, started, jobId)
VALUES ("${data.category}", "running", "May", "${data.uuid}") RETURNING id;`, [], callback)
}
16 changes: 16 additions & 0 deletions courses/models/mapping.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { db } from '../utils/db.js';

const TABLE = 'mappings';

function connection() {
return db.connection;
}

export function All(callback) {
return connection().all(`Select * from ${TABLE}`, [], callback);
}

export function Create(data, callback) {
return connection().run(`Insert into ${TABLE} (source_key, source_value, map_key, map_value, uuid)
VALUES ("${data.sourceKey}", "${data.sourceValue}", "${data.mapKey}", "${data.mapValue}", "${data.uuid}")`, [], callback)
}
10 changes: 10 additions & 0 deletions courses/models/rule.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// const { db } = require('../utils/db');
import { db } from '../utils';

// DB model for rules
module.exports = {
table: 'rules',
connection() { return db.connection },
all() { return this.connection().run(`Select * from ${this.table}`) },
create(data) { this.connection().run(`Insert into ${this.table} (name, rules) VALUES (${data.name, data.rules})`) }
}
Loading