Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/atlp-rwanda/eagles-ec-be int…
Browse files Browse the repository at this point in the history
…o ft-user-signup-#187431242
  • Loading branch information
yvanddniyo committed Apr 18, 2024
2 parents 9f792ba + a44a22c commit 28c501c
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 36 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Eagle e-commerce CI/CD

on:
push:
branches:
- main
- dev

pull_request:
branches:
- main
- dev

workflow_dispatch:

jobs:
build:
name: Building code
runs-on: ubuntu-latest

steps:
- name: Checkout the code
uses: actions/checkout@v3

- name: Setup node
uses: actions/setup-node@v3
with:
node-version: "20"

- name: Install dependencies
run: npm install

- name: Running test
env:
DB_CONNECTION: ${{ secrets.DB_CONNECTION }}
TEST_DB: ${{ secrets.TEST_DB }}
run: npm run test

- name: Build application
run: npm run build

- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: soleil00/eagles-ec-be

- name: Trigger Render Deployment
env:
RENDER_DEPLOYMENT_HOOK_URL: ${{ secrets.RENDER_DEPLOYMENT_HOOK_URL }}
run: |
curl -X POST $RENDER_DEPLOYMENT_HOOK_URL
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
# eagles-ec-be

<div style="display: flex; justify-content: center;" align="center">
<img src="https://codecov.io/gh/soleil00/eagles-ec-be/branch/dev/graph/badge.svg?token=9c1e8e93-1062-4e49-a58d-b2777a75fb70" alt="Codecov" >
<img alt="GitHub Actions Workflow Status" src="https://img.shields.io/github/actions/workflow/status/soleil00/eagles-ec-be/test.yml" >
<a href="https://codeclimate.com/github/atlp-rwanda/eagles-ec-be/maintainability"><img src="https://api.codeclimate.com/v1/badges/dfe8454356fb9da65407/maintainability" /></a>

</div>

### Technology used

![Node.js](https://img.shields.io/badge/-Node.js-000000?style=flat&logo=node.js)
[![Passport.js](https://img.shields.io/badge/auth%20library-Passport.js-green)](http://www.passportjs.org/)
[![PostgreSQL](https://img.shields.io/badge/database-PostgreSQL-blue)](https://www.postgresql.org/)
[![Sequelize](https://img.shields.io/badge/ORM-Sequelize-orange)](https://sequelize.org/)
[![Jest](https://img.shields.io/badge/testing-Jest-red)](https://jestjs.io/)
[![ESLint](https://img.shields.io/badge/code%20style-ESLint-blueviolet)](https://eslint.org/)

### Deployed link

[Eagles EC](https://eagles-ec-be-development.onrender.com/)
6 changes: 3 additions & 3 deletions __test__/home.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import request from "supertest";
import { beforeAll, afterAll, jest, test } from "@jest/globals";
import app from "../src/utils/server";
import { testDbConnection, testSequelize } from "../src/config/testDbConfig";
import sequelize, { connect } from "../src/config/dbConnection";

describe("Testing Home route", () => {
beforeAll(async () => {
try {
await testDbConnection();
await connect();
} catch (error) {
testSequelize.close();
sequelize.close();
}
}, 20000);

Expand Down
7 changes: 3 additions & 4 deletions __test__/user.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import request from "supertest";
import { beforeAll, afterAll, jest, test } from "@jest/globals";
import app from "../src/utils/server";
import { testDbConnection, testSequelize } from "../src/config/testDbConfig";
import User from "../src/sequelize/models/users";
import * as userServices from "../src/services/user.service";
import sequelize, { connect } from "../src/config/dbConnection";

describe("Testing user Routes", () => {
beforeAll(async () => {
try {
await testDbConnection();
await connect();
} catch (error) {
testSequelize.close();
sequelize.close();
}
}, 20000);

Expand All @@ -20,6 +20,5 @@ describe("Testing user Routes", () => {
const response = await request(app).get("/api/v1/users");
expect(spy).toHaveBeenCalled();
expect(spy2).toHaveBeenCalled();
expect(response.statusCode).toBe(200);
}, 20000);
});
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ app.listen(env.port, async () => {
await sequelize
.sync()
.then(() => {
console.log(" db synced and server is running");
console.log(` db synced and server is running on port ${env.port}`);
})
.catch((error: any) => {
console.log(error.message);
Expand Down
18 changes: 9 additions & 9 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testMatch: ["**/**/*.test.ts"],
verbose: true,
forceExit: true,
clearMocks: true,
resetMocks: true,
restoreMocks: true,
};
preset: "ts-jest",
testEnvironment: "node",
testMatch: ["**/**/*.test.ts"],
verbose: true,
forceExit: true,
clearMocks: true,
resetMocks: true,
restoreMocks: true,
};
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"seed": "npx sequelize-cli db:seed:all",
"lint": "npx eslint .",
"lint:fix": "npx eslint --fix .",
"test": "jest --detectOpenHandles --coverage"
"test": "cross-env NODE_ENV=test jest --detectOpenHandles --coverage"
},
"author": "atlp",
"license": "MIT",
Expand All @@ -23,6 +23,7 @@
"@types/jest": "^29.5.12",
"@types/node": "^20.12.7",
"@types/supertest": "^6.0.2",
"@types/swagger-ui-express": "^4.1.6",
"@typescript-eslint/eslint-plugin": "^7.6.0",
"@typescript-eslint/parser": "^7.6.0",
"eslint": "^8.57.0",
Expand All @@ -44,11 +45,13 @@
"dependencies": {
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"path": "^0.12.7",
"pg": "^8.11.5",
"pg-hstore": "^2.3.4",
"sequelize": "^6.37.2"
"sequelize": "^6.37.2",
"swagger-ui-express": "^5.0.0"
}
}
4 changes: 3 additions & 1 deletion src/config/dbConnection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Sequelize } from "sequelize";
import { env } from "../utils/env";

const sequelize = new Sequelize(env.db_url);
const envT = process.env.NODE_ENV;

const sequelize = new Sequelize(envT === "test" ? env.test_db_url : env.db_url);

export const connect = async () => {
try {
Expand Down
11 changes: 0 additions & 11 deletions src/config/testDbConfig.ts

This file was deleted.

55 changes: 55 additions & 0 deletions src/docs/swagger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import express from "express";
import { serve, setup } from "swagger-ui-express";
import { env } from "../utils/env";
import { getUsers, userSchema } from "./users";

const docRouter = express.Router();

const options = {
openapi: "3.0.1",
info: {
title: "Eagles E-commerce API",
version: "1.0.0",
description: "Documentation for Eagles E-commerce Backend",
},

servers: [{
url: `http://localhost:${env.port}`,
description: 'Development server',
}, {
url: 'https://eagles-ec-be-development.onrender.com/',
description: 'Production server',
}],

basePath: "/",

tags: [
{ name: "Users", description: "Endpoints related to users" }
],

paths: {
"/api/v1/users": {
get: getUsers
}
},

components: {
schemas: {
User: userSchema,
},
securitySchemes: {
bearerAuth: {
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
in: "header",
name: "Authorization",
},
},
}

}

docRouter.use("/", serve, setup(options));

export default docRouter
38 changes: 38 additions & 0 deletions src/docs/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export const userSchema = {
type: "object",
properties: {
name: {
type: "string",
},
username: {
type: "string"
},
email: {
type: "string",
format: "email",
},
password: {
type: "string",
},
},
}

export const getUsers = {
tags: ["Users"],
summary: "Get all users",
responses: {
200: {
description: "OK",
content: {
"application/json": {
schema: {
type: "array",
items: {
$ref: "#/components/schemas/User",
},
},
},
},
},
},
}
2 changes: 1 addition & 1 deletion src/sequelize/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
},
},
test: {
url: process.env.DB_CONNECTION,
url: process.env.TEST_DB,
dialect: "postgres",
dialectOptions: {
ssl: {
Expand Down
4 changes: 2 additions & 2 deletions src/sequelize/models/users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Model,DataTypes} from 'sequelize';
import { testSequelize } from '../../config/testDbConfig';
import sequelize from '../../config/dbConnection';

export interface UserAttributes{
id?:number,
Expand Down Expand Up @@ -53,7 +53,7 @@ class User extends Model<UserAttributes> implements UserAttributes{
type: DataTypes.DATE,
},
}, {
sequelize:testSequelize,
sequelize:sequelize,
modelName: 'users',
});

Expand Down
5 changes: 3 additions & 2 deletions src/sequelize/models/usersTests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Model,DataTypes} from 'sequelize';
import { testSequelize } from '../../config/testDbConfig';
import sequelize from '../../config/dbConnection';


export interface UserAttributes{
id?:number,
Expand Down Expand Up @@ -53,7 +54,7 @@ class UserTest extends Model<UserAttributes> implements UserAttributes{
type: DataTypes.DATE,
},
}, {
sequelize:testSequelize,
sequelize:sequelize,
modelName: 'usersTests',
});

Expand Down
1 change: 1 addition & 0 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ dotenv.config();
export const env = {
port: process.env.PORT || 3000,
db_url: process.env.DB_CONNECTION as string,
test_db_url: process.env.TEST_DB as string,
};
2 changes: 2 additions & 0 deletions src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import cors from "cors";
import appROutes from "../routes";
import userRoutes from "../routes/userRoutes";
import homeRoute from "../routes/homeRoutes";
import docRouter from "../docs/swagger";

const app = express();

Expand All @@ -14,5 +15,6 @@ app.use(cors());
app.use("/", homeRoute);
app.use("/api/v1", appROutes);
app.use("/api/v1", userRoutes);
app.use("/docs", docRouter);

export default app;

0 comments on commit 28c501c

Please sign in to comment.