Skip to content

Commit

Permalink
Merge pull request #85 from incredible-phoenix246/chore/unit-testing
Browse files Browse the repository at this point in the history
Chore/unit testing
  • Loading branch information
Idimmusix authored Jul 22, 2024
2 parents 49a76e7 + d3efa2f commit 199db35
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 141 deletions.
10 changes: 8 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
port=8000
PORT=8000
AUTH_SECRET=
DB_USER=
DB_HOST=
DB_PASSWORD=
DB_NAME=
DB_NAME=
NODE_ENV=development
SMTP_USER=
SMTP_PASSWORD=
SMTP_HOST=
SMTP_SERVICE=

11 changes: 11 additions & 0 deletions db/migrations/1721611425287-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class Migration1721611425287 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
}

public async down(queryRunner: QueryRunner): Promise<void> {
}

}
11 changes: 7 additions & 4 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ const config: Config = {
verbose: true,
preset: "ts-jest",
testEnvironment: "node",
globals: {
"ts-jest": {
tsconfig: "tsconfig.jest.json",
},
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
tsconfig: "tsconfig.jest.json",
},
],
},
};

Expand Down
16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,35 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "commonjs",
"scripts": {
"start:dev": "ts-node-dev --respawn --transpile-only ./src/index",
"start": "ts-node --transpile-only src/index.ts",
"test": "jest ",
"typeorm": "typeorm-ts-node-commonjs",
"build": "tsc",
"prod": "node dist/index.js",
"migrate": "typeorm migration:run -d src/data-source.ts"
"migrate": "typeorm-ts-node-commonjs migration:run -d src/data-source",
"migration:create": "typeorm-ts-node-commonjs migration:create db/migrations/migration",
"migration:generate": "typeorm-ts-node-commonjs migration:generate db/migrations/migration -d src/data-source",
"migration:revert": "typeorm-ts-node-commonjs migration:revert -d src/data-source"
},
"repository": {
"type": "git",
"url": "https://github.com/hngprojects/hng_boilerplate_expressjs"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/hngprojects/hng_boilerplate_expressjs/issues"
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/jest": "^29.5.12",
"@types/node": "^16.18.103",
"@types/supertest": "^6.0.2",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.6",
"ts-node": "^10.9.1",
"ts-node": "^10.9.2",
"typescript": "^5.5.3"
},
"dependencies": {
Expand Down
2 changes: 2 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ const config = {
SMTP_PASSWORD: process.env.SMTP_PASSWORD,
SMTP_HOST: process.env.SMTP_HOST,
SMTP_SERVICE: process.env.SMTP_SERVICE,
NODE_ENV: process.env.NODE_ENV,
TWILIO_SID: process.env.TWILIO_SID,
TWILIO_AUTH_TOKEN: process.env.TWILIO_AUTH_TOKEN,
TWILIO_PHONE_NUMBER: process.env.TWILIO_PHONE_NUMBER,

};

export default config;
16 changes: 14 additions & 2 deletions src/data-source.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import "reflect-metadata";
import { DataSource, Tree } from "typeorm";
import { DataSource } from "typeorm";
import config from "./config";

const isDevelopment = config.NODE_ENV === "development";

const AppDataSource = new DataSource({
type: "postgres",
host: config.DB_HOST,
port: 5432,
username: config.DB_USER,
password: config.DB_PASSWORD,
database: config.DB_NAME,
synchronize: true,
synchronize: isDevelopment,
logging: false,
entities: ["src/models/**/*.ts"],
migrations: ["src/migrations/**/*.ts"],
migrationsTableName: "migrations",
ssl: false,
extra: {
ssl: {
Expand All @@ -20,4 +24,12 @@ const AppDataSource = new DataSource({
},
});

export async function initializeDataSource() {
if (!AppDataSource.isInitialized) {
await AppDataSource.initialize();
}
return AppDataSource;
}

export default AppDataSource;

2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import {
notificationRouter,
smsRouter,
} from "./routes";

import { routeNotFound, errorHandler } from "./middleware";
// import { seed } from "./seeder";
import { orgRouter } from "./routes/organisation";
import swaggerUi from "swagger-ui-express";
import swaggerSpec from "./swaggerConfig";
Expand Down
3 changes: 0 additions & 3 deletions src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ export class User extends ExtendedBaseEntity {
@JoinTable()
organizations: Organization[];

@OneToMany(() => Sms, (sms) => sms.sender, { cascade: true })
sms: Sms[];

@CreateDateColumn()
createdAt: Date;

Expand Down
8 changes: 4 additions & 4 deletions src/services/OrgService.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Organization } from "../models/organization";
import { AppDataSource } from "../data-source";
import AppDataSource from "../data-source";
import { User } from "../models/user";
import { IOrgService, IUserService } from "../types";

export class OrgService implements IOrgService {
public async removeUser(
org_id: string,
user_id: string,
user_id: string
): Promise<User | null> {
const userRepository = AppDataSource.getRepository(User);
const organizationRepository = AppDataSource.getRepository(Organization);
Expand All @@ -29,15 +29,15 @@ export class OrgService implements IOrgService {

// Check if the user is part of the organization
const userInOrganization = organization.users.some(
(user) => user.id === user_id,
(user) => user.id === user_id
);
if (!userInOrganization) {
return null;
}

// Remove the user from the organization
organization.users = organization.users.filter(
(user) => user.id !== user_id,
(user) => user.id !== user_id
);
await organizationRepository.save(organization);

Expand Down
Loading

0 comments on commit 199db35

Please sign in to comment.