Skip to content
Merged
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
15,577 changes: 12,633 additions & 2,944 deletions backend/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@ericblade/quagga2": "^1.8.2",
"@types/passport": "^1.0.12",
"axios": "^1.3.4",
"axios": "^1.4.0",
"bcrypt": "^5.0.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
Expand Down
1,104 changes: 1,104 additions & 0 deletions backend/sql/PLU_data.sql

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions backend/sql/create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ CREATE TABLE Shelf (
FOREIGN KEY (section_id)
REFERENCES Section(section_id)
);
CREATE TYPE category AS ENUM ('produce', 'fruit', 'meat', 'dairy', 'baked goods', 'canned', 'snacks', 'beverage', 'condiments & spices', 'processed foods', 'other' );

CREATE TABLE Item (
item_id serial,
name text UNIQUE,
category category,
name text,
category text,
stor_id int NOT NULL,
size int,
barcode text UNIQUE,
Expand Down
67 changes: 0 additions & 67 deletions backend/sql/dummy_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,70 +42,3 @@ VALUES ((SELECT MAX(site_id) FROM Site), 'Produce Aisle');
INSERT INTO Section (aisle_id, stor_id)
VALUES ((SELECT MAX(aisle_id) FROM Aisle), (SELECT MAX(stor_id) FROM Storage_type)),
((SELECT MAX(aisle_id) FROM Aisle), (SELECT MAX(stor_id) FROM Storage_type)-1);

-- Insert test data for Shelf
INSERT INTO Shelf (section_id, capacity)
VALUES ((SELECT MAX(section_id) FROM Section), 100),
((SELECT MAX(section_id) FROM Section), 50),
((SELECT MAX(section_id) FROM Section)-1, 75),
((SELECT MAX(section_id) FROM Section)-1, 150);

-- Insert test data for Aisle
INSERT INTO Aisle (site_id, info)
VALUES ((SELECT MAX(site_id) FROM Site), 'Dairy Aisle');

-- Insert test data for Section
INSERT INTO Section (aisle_id, stor_id)
VALUES ((SELECT MAX(aisle_id) FROM Aisle), (SELECT MAX(stor_id) FROM Storage_type)-2),
((SELECT MAX(aisle_id) FROM Aisle), (SELECT MAX(stor_id) FROM Storage_type)-2);

-- Insert test data for Shelf
INSERT INTO Shelf (section_id, capacity)
VALUES ((SELECT MAX(section_id) FROM Section), 100),
((SELECT MAX(section_id) FROM Section), 50),
((SELECT MAX(section_id) FROM Section)-1, 75),
((SELECT MAX(section_id) FROM Section)-1, 150);

-- Insert test data for Item
INSERT INTO Item (item_id, name, category, stor_id, size, barcode)
VALUES (1, 'Apples', 'produce', (SELECT MAX(stor_id) FROM Storage_type)-1, 10, '123456789'),
(2, 'Oranges', 'produce', (SELECT MAX(stor_id) FROM Storage_type), 15,'223456789'),
(3, 'Milk', 'dairy', (SELECT MAX(stor_id) FROM Storage_type)-2, 1, '323456789'),
(4, 'Cheese', 'dairy', (SELECT MAX(stor_id) FROM Storage_type)-2, 0.5, '423456789');

-- Insert test data for Permissions
INSERT INTO Permissions (perm_num, description)
VALUES (1, 'Can view inventory'),
(2, 'Can edit inventory');

-- Insert test data for Empl_info
INSERT INTO Empl_info (perm_id, role)
VALUES ((SELECT MAX(perm_id) FROM Permissions), 'volunteer');

UPDATE Person SET empl_id = (SELECT MAX(empl_id) FROM Empl_info)
WHERE email = 'johndoe@example.com';

-- Insert test data for Transaction
INSERT INTO Transaction (person_id, date, trans_type, site)
VALUES ((SELECT MAX(person_id) FROM Person), NOW(), 'donation', 1),
-- Add old transaction with goods that have been expiring.
((SELECT MIN(person_id) FROM Person), NOW() - INTERVAL '3 weeks', 'donation', 1);

-- Insert test data for Trans_items
INSERT INTO Trans_items (trans_id, item_id, quantity, expiration)
VALUES ((SELECT MIN(trans_id) FROM Transaction), 4, 5, NOW() + INTERVAL '1 week'),
((SELECT MIN(trans_id) FROM Transaction), 3, 10, NOW() + INTERVAL '2 weeks'),
-- Donate goods that expired 3 weeks ago, 1 week ago, and 1 week from now.
-- Old apples
((SELECT MAX(trans_id) FROM Transaction), 1, 7, NOW() - INTERVAL '3 weeks'),
-- Old oranges
((SELECT MAX(trans_id) FROM Transaction), 2, 7, NOW() - INTERVAL '1 week'),
-- Nearly old Milk
((SELECT MAX(trans_id) FROM Transaction), 3, 7, NOW() + INTERVAL '1 day');

-- Insert test data for Shelf_contents
INSERT INTO Shelf_contents (trans_item_id, shelf_id, store_date, quantity)
VALUES ((SELECT MAX(trans_item_id) FROM Trans_items), (SELECT MAX(shelf_id) FROM Shelf), NOW(), 5),
((SELECT MAX(trans_item_id) FROM Trans_items), (SELECT MAX(shelf_id) FROM Shelf), NOW(), 10);

REFRESH MATERIALIZED VIEW stock;
59 changes: 59 additions & 0 deletions backend/sql/dummy_data2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
-- Insert test data for Shelf
INSERT INTO Shelf (section_id, capacity)
VALUES ((SELECT MAX(section_id) FROM Section), 100),
((SELECT MAX(section_id) FROM Section), 50),
((SELECT MAX(section_id) FROM Section)-1, 75),
((SELECT MAX(section_id) FROM Section)-1, 150);

-- Insert test data for Aisle
INSERT INTO Aisle (site_id, info)
VALUES ((SELECT MAX(site_id) FROM Site), 'Dairy Aisle');

-- Insert test data for Section
INSERT INTO Section (aisle_id, stor_id)
VALUES ((SELECT MAX(aisle_id) FROM Aisle), (SELECT MAX(stor_id) FROM Storage_type)-2),
((SELECT MAX(aisle_id) FROM Aisle), (SELECT MAX(stor_id) FROM Storage_type)-2);

-- Insert test data for Shelf
INSERT INTO Shelf (section_id, capacity)
VALUES ((SELECT MAX(section_id) FROM Section), 100),
((SELECT MAX(section_id) FROM Section), 50),
((SELECT MAX(section_id) FROM Section)-1, 75),
((SELECT MAX(section_id) FROM Section)-1, 150);

-- Insert test data for Permissions
INSERT INTO Permissions (perm_num, description)
VALUES (1, 'Can view inventory'),
(2, 'Can edit inventory');

-- Insert test data for Empl_info
INSERT INTO Empl_info (perm_id, role)
VALUES ((SELECT MAX(perm_id) FROM Permissions), 'volunteer');

UPDATE Person SET empl_id = (SELECT MAX(empl_id) FROM Empl_info)
WHERE email = 'johndoe@example.com';

-- Insert test data for Transaction
INSERT INTO Transaction (person_id, date, trans_type, site)
VALUES ((SELECT MAX(person_id) FROM Person), NOW(), 'donation', 1),
-- Add old transaction with goods that have been expiring.
((SELECT MIN(person_id) FROM Person), NOW() - INTERVAL '3 weeks', 'donation', 1);

-- Insert test data for Trans_items
INSERT INTO Trans_items (trans_id, item_id, quantity, expiration)
VALUES ((SELECT MIN(trans_id) FROM Transaction), 4, 5, NOW() + INTERVAL '1 week'),
((SELECT MIN(trans_id) FROM Transaction), 3, 10, NOW() + INTERVAL '2 weeks'),
-- Donate goods that expired 3 weeks ago, 1 week ago, and 1 week from now.
-- Old apples
((SELECT MAX(trans_id) FROM Transaction), 1, 7, NOW() - INTERVAL '3 weeks'),
-- Old oranges
((SELECT MAX(trans_id) FROM Transaction), 2, 7, NOW() - INTERVAL '1 week'),
-- Nearly old Milk
((SELECT MAX(trans_id) FROM Transaction), 3, 7, NOW() + INTERVAL '1 day');

-- Insert test data for Shelf_contents
INSERT INTO Shelf_contents (trans_item_id, shelf_id, store_date, quantity)
VALUES ((SELECT MAX(trans_item_id) FROM Trans_items), (SELECT MAX(shelf_id) FROM Shelf), NOW(), 5),
((SELECT MAX(trans_item_id) FROM Trans_items), (SELECT MAX(shelf_id) FROM Shelf), NOW(), 10);

REFRESH MATERIALIZED VIEW stock;
14 changes: 3 additions & 11 deletions backend/src/models/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { storage_type, storage_typeId } from './storage_type';
export interface itemAttributes {
item_id: number;
name?: string;
category?: "produce" | "fruit" | "meat" | "dairy" | "baked goods" | "canned" | "snacks" | "beverage" | "condiments & spices" | "processed foods" | "other";
category?: string;
stor_id: number;
size?: number;
barcode?:string; // adding the barcode field
Expand All @@ -19,7 +19,7 @@ export type itemCreationAttributes = Optional<itemAttributes, itemOptionalAttrib
export class item extends Model<itemAttributes, itemCreationAttributes> implements itemAttributes {
item_id!: number;
name?: string;
category?: "produce" | "fruit" | "meat" | "dairy" | "baked goods" | "canned" | "snacks" | "beverage" | "condiments & spices" | "processed foods" | "other";
category?: string
stor_id!: number;
size?: number;
barcode?: string;
Expand All @@ -45,10 +45,9 @@ export class item extends Model<itemAttributes, itemCreationAttributes> implemen
name: {
type: DataTypes.TEXT,
allowNull: true,
unique: "item_name_key"
},
category: {
type: DataTypes.ENUM("produce","fruit","meat","dairy","baked goods","canned","snacks","beverage","condiments & spices","processed foods","other"),
type: DataTypes.TEXT,
allowNull: true
},
stor_id: {
Expand All @@ -73,13 +72,6 @@ export class item extends Model<itemAttributes, itemCreationAttributes> implemen
schema: 'public',
timestamps: false,
indexes: [
{
name: "item_name_key",
unique: true,
fields: [
{ name: "name" },
]
},
{
name: "item_pkey",
unique: true,
Expand Down
17 changes: 0 additions & 17 deletions backend/src/routes/barcode.js

This file was deleted.

54 changes: 54 additions & 0 deletions backend/src/routes/barcode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var express = require('express');
var router = express.Router();
const { item } = require("../models/init-models");
const axios = require("axios");
var { ensureAuthenticated } = require('../config/auth');

const axiosInstance = axios.create({
baseURL: "https://world.openfoodfacts.org/api/v2",
timeout: 3000
});



async function UPC_lookup(barcode: string) {
try {
var scannedItem = await axiosInstance.get('/search', {params: {code: barcode}});
var product_name = scannedItem.data.products[0].product_name_en;
return product_name;
} catch (error) {
console.error(error);
return null;
}
}

async function PLU_lookup(barcode: string) {
let product = await item.LookUpbarcode(barcode);
if (product !== null){
return JSON.stringify(product);
}
else {
return null
}
}

router.get('/barcode', ensureAuthenticated, async (req: any, res: any) => {
if (!req.isAuthenticated()) {
const errors = [];
res.post('Unauthenticated');
} else {
const barcode = req.query.barcode;
if (barcode.length > 4) {
const result = await UPC_lookup(barcode);
res.send(result);
} else if (barcode.length === 4) {
const result = await PLU_lookup(barcode);
res.send(result);
}
else {
return res.status(404).send('Item not found');
}
}
});

module.exports = router;
2 changes: 1 addition & 1 deletion backend/src/routes/stock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var express = require('express');
var router = express.Router();
const { ensureAuthenticated } = require('../config/auth');
var { ensureAuthenticated } = require('../config/auth');

var { Sequelize } = require('sequelize');
var { initModels, stock } = require("../models/init-models");
Expand Down
6 changes: 4 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ services:
image: postgres:latest
volumes:
- ./backend/postgres-data:/var/lib/postgresql/data
- ./backend/sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql
- ./backend/sql/dummy_data.sql:/docker-entrypoint-initdb.d/dummy_data.sql
- ./backend/sql/create_tables.sql:/docker-entrypoint-initdb.d/1_create_tables.sql
- ./backend/sql/dummy_data.sql:/docker-entrypoint-initdb.d/2_dummy_data.sql
- ./backend/sql/PLU_data.sql:/docker-entrypoint-initdb.d/3_PLU_data.sql
- ./backend/sql/dummy_data2.sql:/docker-entrypoint-initdb.d/4_dummy_data.sql
networks:
- local
ports:
Expand Down
28 changes: 28 additions & 0 deletions frontend/src/Components/Barcode/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import axiosInstance from "../../util/axiosInstance";

export default async function lookup(code: string) {
const openFFData = await axiosInstance.get(`/barcode`, {params: {barcode: code}})
.then((res: any) => {
return res;
})
.catch(error => {
console.log("Error in Scanner: " + error);
});
if (openFFData !== "") {
console.log("Found the data: ");
console.log(openFFData);
return openFFData
}
else {
console.log("Product not found");
const promptName = window.prompt("Please enter product name", "");
if (promptName === null || promptName === "" || promptName.length <= 0)
{
console.log("Bad Prompt Input");
}
}


//send to backend make it do all the fun stuffs
}

10 changes: 5 additions & 5 deletions frontend/src/pages/scanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import Box from "@mui/material/Box";
import CircularProgress from "@mui/material/CircularProgress";
import { useAppSelector } from "../hooks";
import { Button, Typography } from "@mui/material";
import lookup from "../Components/Barcode";

function Scanner() {
// const [camera, setCamera] = useState(false);
const [result, setResult] = useState<string | null>(null);
const loading = useAppSelector((state) => state.user.status);
const searching = useAppSelector((state) => state.user.status);

const onDetected = (result: string) => {
setResult(result);
Expand All @@ -21,7 +21,8 @@ function Scanner() {
barcode: "",
},
onSubmit: (values) => {
alert(JSON.stringify(values, null, 2));
console.log("Barcode: " + values.barcode);
lookup(values.barcode);
},
});

Expand Down Expand Up @@ -70,14 +71,13 @@ function Scanner() {
type="submit"
fullWidth
variant="contained"
// disabled={!(formik.dirty && formik.isValid)}
disabled={!(result || formik.values.barcode)}
sx={{ mt: 3, mb: 2, py: 2 }}
style={{
backgroundColor: "primary",
}}>
Lookup
{loading === "loading" && <CircularProgress />}
{searching === "searching" && <CircularProgress />}
</Button>
</Box>
</div>
Expand Down
Loading