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

Summary page API's #156

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion backend/sql/create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ CREATE TABLE Trans_items (
PRIMARY KEY (trans_item_id),
CONSTRAINT FK_Trans_items_trans_id
FOREIGN KEY (trans_id)
REFERENCES Transaction(trans_id)
REFERENCES Transaction(trans_id),
CONSTRAINT FK_Trans_items_item_id
FOREIGN KEY (item_id)
REFERENCES Item(item_id)
);

CREATE TABLE Shelf_contents (
Expand Down
6 changes: 6 additions & 0 deletions backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ require('./config/passport')(passport);
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var saleRouter = require('./routes/sale');
var summaryRouter = require('./routes/summary');

var app = express();
require("uuid");

Expand Down Expand Up @@ -72,6 +74,10 @@ app.get('/checkout', saleRouter);
app.get('/charts', saleRouter);
app.post('/checkout', saleRouter);

app.get('/purchases', summaryRouter);
app.get('/currentstock', summaryRouter);
app.get('/wastemanagement', summaryRouter);
app.get('/soontoexpire', summaryRouter);

app.get('/checkout_success', function(req, res, next) {

Expand Down
1 change: 1 addition & 0 deletions backend/src/models/init-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export function initModels(sequelize: typeof Sequelize) {
shelf_contents.belongsTo(trans_items, { as: "trans_item", foreignKey: "trans_item_id"});
trans_items.hasMany(shelf_contents, { as: "shelf_contents", foreignKey: "trans_item_id"});
trans_items.belongsTo(transaction, { as: "tran", foreignKey: "trans_id"});
trans_items.belongsTo(item, { as: "item", foreignKey: "item_id" });
transaction.hasMany(trans_items, { as: "trans_items", foreignKey: "trans_id"});

return {
Expand Down
6 changes: 6 additions & 0 deletions backend/src/models/trans_items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Sequelize from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';
import type { shelf_contents, shelf_contentsId } from './shelf_contents';
import type { transaction, transactionId } from './transaction';
import type { item, itemId } from './item';

export interface trans_itemsAttributes {
trans_id: number;
Expand Down Expand Up @@ -41,6 +42,11 @@ export class trans_items extends Model<trans_itemsAttributes, trans_itemsCreatio
setTran!: Sequelize.BelongsToSetAssociationMixin<transaction, transactionId>;
createTran!: Sequelize.BelongsToCreateAssociationMixin<transaction>;

item!: item;
getItem!: Sequelize.BelongsToGetAssociationMixin<item>;
setItem!: Sequelize.BelongsToSetAssociationMixin<item, itemId>;
createItem!: Sequelize.BelongsToCreateAssociationMixin<item>;

static initModel(sequelize: Sequelize.Sequelize): typeof trans_items {
return trans_items.init({
trans_id: {
Expand Down
173 changes: 173 additions & 0 deletions backend/src/routes/summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
var express = require('express');
var router = express.Router();
const { ensureAuthenticated } = require('../config/auth');

var { Sequelize, Op } = require('sequelize');
var { initModels, item, trans_items, transaction } = require("../models/init-models");

const sequelize = new Sequelize(require('../config/keys').PostgresURI);

initModels(sequelize);

router.get('/purchases', ensureAuthenticated, function (req, res) {
if (!req.isAuthenticated()) {
const errors = [];
res.post('Unauthenticated');
} else {

trans_items.findAll({
include: [{
model: transaction,
as: 'tran',
required: true,
attributes: ['date']
}, {
model: item,
as: 'item',
required: true,
attributes: ['name']
}],
attributes: ['trans_item_id', 'item.name','item.category','quantity','tran.date'],
}).then(transItems => {
const result = transItems.map(transItem => {
return {
trans_item_id: transItem.trans_item_id,
name: transItem.item.name,
type: transItem.item.category,
quantity: transItem.quantity,
date: transItem.tran.date
};
});
if (result == null) {
console.log("THIS IS ERROR " + result);
} else {
res.json(JSON.stringify(result));
}
});
}
});

router.get('/currentstock', ensureAuthenticated, function (req, res) {
if (!req.isAuthenticated()) {
const errors = [];
res.post('Unauthenticated');
} else {

trans_items.findAll({
include: [{
model: transaction,
as: 'tran',
required: true,
attributes: ['date']
}, {
model: item,
as: 'item',
required: true,
attributes: ['name']
}],
attributes: ['trans_item_id', 'item.name','item.category','tran.date','expiration'],
}).then(transItems => {
const result = transItems.map(transItem => {
return {
trans_item_id: transItem.trans_item_id,
name: transItem.item.name,
type: transItem.item.category,
stocked_date: transItem.tran.date,
expiry_date: transItem.expiration
};
});
if (result == null) {
console.log("THIS IS ERROR " + result);
} else {
res.json(JSON.stringify(result));
}
});
}
});

router.get('/wastemanagement', ensureAuthenticated, function (req, res) {
if (!req.isAuthenticated()) {
const errors = [];
res.post('Unauthenticated');
} else {
const currentDate = new Date();

trans_items.findAll({
where: {
expiration: {
[Op.lt]: currentDate
}
},
include: [{
model: transaction,
as: 'tran',
required: true,
attributes: ['date']
}, {
model: item,
as: 'item',
required: true,
attributes: ['name']
}],
attributes: ['trans_item_id','quantity','item.name','expiration'
],}).then(transItems => {
const now = new Date();
const result = transItems.map(transItem => {
return {
trans_item_id: transItem.trans_item_id,
name: transItem.item.name,
quantity: transItem.quantity,
expiration: transItem.expiration
};
});
if (result == null) {
console.log("THIS IS ERROR " + result);
} else {
res.json(JSON.stringify(result));
}
});
}
});

router.get('/soontoexpire', ensureAuthenticated, function (req, res) {
if (!req.isAuthenticated()) {
const errors = [];
res.post('Unauthenticated');
} else {
const twoDaysFromNow = new Date();
twoDaysFromNow.setDate(twoDaysFromNow.getDate() + 2);

trans_items.findAll({
// where: {
// expiration: {
// [Op.lte]: twoDaysFromNow
// }
// },
include: [{
model: item,
as: 'item',
required: true,
attributes: ['name']
}],
attributes: ['trans_item_id', 'expiration','item.name'],
}).then(transItems => {
const now = new Date();
const result = transItems.map(transItem => {
const expirationDiff = Math.ceil((new Date(transItem.expiration).getTime() - new Date().getTime()) / (1000 * 60 * 60 * 24));
return {
trans_item_id: transItem.trans_item_id,
Name: transItem.item.name,
No_of_days_remaining: expirationDiff,
expiration: transItem.expiration
};
});
if (result == null) {
console.log("THIS IS ERROR " + result);
} else {
res.json(JSON.stringify(result));
}
});
}
});

module.exports = router;