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

WIP: OpenHarvest-13 prepare backend for production with integrations confi… #14

Open
wants to merge 3 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
243 changes: 240 additions & 3 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
{
"name": "OpenHarvest-backend",
"name": "@openharvest/backend",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon",
"build": "tsc",
"build-ts": "tsc --build",
"build": "npm run build-ts && npm run lint",
"debug": "npm run build && npm run watch-debug",
"lint": "tsc --noEmit && eslint \"**/*.{js,ts}\" --quiet --fix",
"serve-debug": "nodemon --inspect dist/server.js",
"serve": "node dist/server.js",
"start": "npm run serve",
"watch-debug": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-ts\" \"npm run serve-debug\"",
"watch-node": "nodemon dist/server.js",
"watch-test": "npm run test -- --watchAll",
"watch-ts": "tsc --build -w ",
"watch": "npm run build-ts && concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-ts\" \"npm run watch-node\"",
"mkcert": "mkcert localhost 127.0.0.1"
},
"keywords": [],
Expand All @@ -30,11 +40,238 @@
"uuid": "^8.3.2"
},
"devDependencies": {
"@types/cookie-parser": "^1.4.3",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/express-session": "^1.17.4",
"@types/passport": "^1.0.7",
"concurrently": "^7.2.0",
"nodemon": "^2.0.15",
"ts-node": "^10.5.0",
"typescript": "^4.5.5"
},
"eslintConfig": {
"overrides": [
{
"files": [
"**/*.ts?(x)"
],
"extends": [
"plugin:react/recommended"
],
"plugins": [
"eslint-plugin-prefer-arrow",
"eslint-plugin-react",
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/adjacent-overload-signatures": "warn",
"@typescript-eslint/array-type": [
"warn",
{
"default": "array"
}
],
"@typescript-eslint/ban-types": [
"warn",
{
"types": {
"Object": {
"message": "Avoid using the `Object` type. Did you mean `object`?"
},
"Function": {
"message": "Avoid using the `Function` type. Prefer a specific function type, like `() => void`."
},
"Boolean": {
"message": "Avoid using the `Boolean` type. Did you mean `boolean`?"
},
"Number": {
"message": "Avoid using the `Number` type. Did you mean `number`?"
},
"String": {
"message": "Avoid using the `String` type. Did you mean `string`?"
},
"Symbol": {
"message": "Avoid using the `Symbol` type. Did you mean `symbol`?"
}
}
}
],
"@typescript-eslint/consistent-type-assertions": "warn",
"@typescript-eslint/dot-notation": "off",
"@typescript-eslint/explicit-function-return-type": "warn",
"@typescript-eslint/explicit-member-accessibility": [
"off",
{
"accessibility": "explicit"
}
],
"@typescript-eslint/indent": "warn",
"@typescript-eslint/member-delimiter-style": [
"warn",
{
"multiline": {
"delimiter": "semi",
"requireLast": true
},
"singleline": {
"delimiter": "semi",
"requireLast": false
}
}
],
"@typescript-eslint/member-ordering": "warn",
"@typescript-eslint/naming-convention": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-empty-interface": "warn",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-inferrable-types": [
"warn",
{
"ignoreParameters": true
}
],
"@typescript-eslint/no-misused-new": "warn",
"@typescript-eslint/no-namespace": "warn",
"@typescript-eslint/no-parameter-properties": "off",
"@typescript-eslint/no-shadow": [
"warn",
{
"hoist": "all"
}
],
"@typescript-eslint/no-unused-expressions": "warn",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-var-requires": "warn",
"@typescript-eslint/prefer-for-of": "warn",
"@typescript-eslint/prefer-function-type": "warn",
"@typescript-eslint/prefer-namespace-keyword": "warn",
"@typescript-eslint/quotes": [
"warn",
"double"
],
"@typescript-eslint/semi": [
"warn",
"always"
],
"@typescript-eslint/triple-slash-reference": [
"warn",
{
"path": "always",
"types": "prefer-import",
"lib": "always"
}
],
"@typescript-eslint/type-annotation-spacing": "warn",
"@typescript-eslint/unified-signatures": "warn",
"brace-style": [
"warn",
"1tbs"
],
"complexity": "off",
"constructor-super": "warn",
"curly": "warn",
"eol-last": "warn",
"eqeqeq": [
"warn",
"smart"
],
"guard-for-in": "warn",
"id-blacklist": [
"warn",
"any",
"Number",
"number",
"String",
"string",
"Boolean",
"boolean",
"Undefined",
"undefined"
],
"id-match": "warn",
"indent": "off",
"max-classes-per-file": [
"warn",
1
],
"max-len": [
"warn",
{
"code": 200
}
],
"new-parens": "warn",
"no-bitwise": "warn",
"no-caller": "warn",
"no-cond-assign": "warn",
"no-console": [
"warn",
{
"allow": [
"log",
"warn",
"dir",
"timeLog",
"assert",
"clear",
"count",
"countReset",
"group",
"groupEnd",
"table",
"dirxml",
"error",
"groupCollapsed",
"Console",
"profile",
"profileEnd",
"timeStamp",
"context"
]
}
],
"no-debugger": "warn",
"no-empty": "off",
"no-eval": "warn",
"no-fallthrough": "warn",
"no-invalid-this": "off",
"no-new-wrappers": "warn",
"no-redeclare": "warn",
"no-restricted-imports": "warn",
"no-throw-literal": "warn",
"no-trailing-spaces": "warn",
"no-undef-init": "warn",
"no-underscore-dangle": "off",
"no-unsafe-finally": "warn",
"no-unused-labels": "warn",
"no-var": "warn",
"object-shorthand": "warn",
"one-var": [
"warn",
"never"
],
"prefer-arrow/prefer-arrow-functions": "warn",
"prefer-const": "warn",
"radix": "warn",
"react/jsx-boolean-value": "warn",
"react/jsx-key": "warn",
"react/jsx-no-bind": "warn",
"react/self-closing-comp": "warn",
"spaced-comment": [
"warn",
"always",
{
"markers": [
"/"
]
}
],
"use-isnan": "warn",
"valid-typeof": "off",
"react/display-name": "warn"
}
}
]
}
}
14 changes: 6 additions & 8 deletions backend/src/auth/IBMiDStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { IDaaSOIDCStrategy as OpenIDConnectStrategy } from "passport-ci-oidc";
import { getCoopManager } from "./../services/coopManager.service";
import { getOrganisations } from "./../services/organisation.service";
import { userService } from "../services/UserService";

export const IBMidStrategy = new OpenIDConnectStrategy({
discoveryURL: process.env.AUTH_discovery_url,
Expand All @@ -11,25 +10,24 @@ export const IBMidStrategy = new OpenIDConnectStrategy({
callbackURL: process.env.AUTH_callback_url,
skipUserProfile: true},
// Add your own data here.
// @ts-ignore
function (iss, sub, profile, accessToken, refreshToken, params, done) {
process.nextTick(async function () {
profile.accessToken = accessToken;
profile.refreshToken = refreshToken;
// Get the farmer coop details
const id = "IBMid:" + profile.id;
const doc = await getCoopManager(id);
const doc = await userService.getUser(id);
if (doc) {
profile.isOnboarded = true;
profile.coopManager = doc.toObject();
profile.organisations = await getOrganisations(doc.coopOrganisations);
profile.selectedOrganisation = profile.organisations[0];
profile.user = doc;
}
else {
profile.isOnboarded = false;
profile.coopManager = null;
profile.user = null;
}
console.log(profile);
done(null, profile);
});
}
)
)
6 changes: 3 additions & 3 deletions backend/src/auth/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CoopManager } from "./../db/entities/coopManager";
import { Organisation } from "./../db/entities/organisation";
import { Organisation, User } from "../../../common-types/src";

export interface CoopManagerUser {
id: string;
Expand All @@ -15,7 +14,7 @@ export interface CoopManagerUser {
exp: number;
accessToken: string;
refreshToken: string;
coopManager: CoopManager | null;
coopManager: User | null;
organisations: Organisation[];
selectedOrganisation: Organisation;
}
Expand All @@ -41,6 +40,7 @@ export function formatUser(user: any): CoopManagerUser {
}
}

// @ts-ignore
export function ensureAuthenticated(req, res, next) {
// console.log(req);
if (!req.isAuthenticated()) {
Expand Down
33 changes: 0 additions & 33 deletions backend/src/db/entities/coopManager.ts

This file was deleted.

22 changes: 5 additions & 17 deletions backend/src/db/entities/crop.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
import { model, Schema, Types } from 'mongoose';

import { Schema, model, ObjectId, Types } from 'mongoose';

const ObjectId = Schema.Types.ObjectId;

export interface Crop {
_id?: Types.ObjectId,
type: string,
name: string,
// Start Day of year to end Day of year when to plant the ground nuts
planting_season: number[],
time_to_harvest: number,
is_ongoing: boolean,
yield_per_sqm: number
}
import { Crop } from "../../../../common-types/src"

// Mongoose will automatically add _id property.
export const CropSchema = new Schema({
export const CropSchema = new Schema<Crop>({
_id: Types.ObjectId,
type: String,
name: String,
// Start Day of year to end Day of year when to plant the ground nuts
planting_season: [Number],
time_to_harvest: Number,
is_ongoing: Boolean,
yield_per_sqm: Number
});

export const CropModel = model<Crop>("crop", CropSchema);
export const CropModel = model<Crop>("crop", CropSchema);
Loading