Skip to content

Commit

Permalink
[Delivers #187584922] Admin should be able to disable an account (#25)
Browse files Browse the repository at this point in the history
* [Delivers #187584922] Admin should be able to disable an account

* Admin should be able to disable or enable an account

* Admin should be able to disable or enable an account

* Admin should be able to disable or enable an account

* Admin should be able to disable or enable an account

* Admin should be able to disable or enable an account

* Adjusted the database

* DB adjusted

* adjusted info

* done rebase
  • Loading branch information
SaddockAime authored May 29, 2024
1 parent f8e8970 commit 64cf94d
Show file tree
Hide file tree
Showing 24 changed files with 466 additions and 142 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ jobs:
- name: Upload coverage to Coveralls
uses: coverallsapp/github-action@v2
env:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
8 changes: 8 additions & 0 deletions .hintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": [
"development"
],
"hints": {
"typescript-config/strict": "off"
}
}
10 changes: 5 additions & 5 deletions .sequelizerc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require("path");
module.exports = {
'config': path.resolve('dist/src','databases', 'config', 'config.js'),
'models-path': path.resolve('dist/src','databases', 'models'),
'seeders-path': path.resolve('dist/src','databases', 'seeders'),
'migrations-path': path.resolve('dist/src','databases', 'migrations')
};
'config': path.resolve('dist/src', 'databases', 'config', 'config.js'),
'models-path': path.resolve('dist/src', 'databases', 'models'),
'seeders-path': path.resolve('dist/src', 'databases', 'seeders'),
'migrations-path': path.resolve('dist/src', 'databases', 'migrations')
};
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@ This is the backend for E-Commerce-Ninjas, written in Node.js with TypeScript.
- Verification Email Endpoint
- Resend verification Endpoint
- Login Endpoint
- Admin change status Endpoint

## TABLE OF API ENDPOINTS SPECIFICATION AND DESCRIPTION


| No | VERBS | ENDPOINTS | STATUS | ACCESS | DESCRIPTION |
|----|-------|------------------------------|-------------|--------|---------------------------|
| 1 | GET | / | 200 OK | public | Show welcome message |
| 2 | POST | /api/auth/register | 201 CREATED | public | create user account |
| 3 | GET | /api/auth/verify-email/:token| 200 OK | public | Verifying email |
| 4 | POST | /api/auth/send-verify-email | 200 OK | public | Resend verification email |
| 4 | POST | /api/auth/login | 200 OK | public | Login with Email and Password |

| No | VERBS | ENDPOINTS | STATUS | ACCESS | DESCRIPTION |
|----|-------|-----------------------------------------|-------------|---------|-------------------------------|
| 1 | GET | / | 200 OK | public | Show welcome message |
| 2 | POST | /api/auth/register | 201 CREATED | public | create user account |
| 3 | GET | /api/auth/verify-email/:token | 200 OK | public | Verifying email |
| 4 | POST | /api/auth/send-verify-email | 200 OK | public | Resend verification email |
| 5 | POST | /api/auth/login | 200 OK | public | Login with Email and Password |
| 6 | PUT | /api/users/admin-update-user-status/:id | 200 OK | private | Admin change status |


## INSTALLATION
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@
"eslint-plugin-import": "^2.29.1",
"lint-staged": "^15.2.2"
}
}
}
2 changes: 1 addition & 1 deletion src/databases/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ const sequelizeConfig = {
}
};

module.exports = sequelizeConfig;
module.exports = sequelizeConfig;
88 changes: 44 additions & 44 deletions src/databases/config/db.config.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
import { config } from "dotenv"
import { Sequelize } from "sequelize"

config()
const NODE_ENV: string = process.env.NODE_ENV || "development"
const DB_HOST_MODE: string = process.env.DB_HOST_TYPE || "remote"

/**
* Get the URI for the database connection.
* @returns {string} The URI string.
*/
function getDbUri(): string {
switch (NODE_ENV) {
case "development":
return process.env.DATABASE_URL_DEV as string
case "test":
return process.env.DATABASE_URL_TEST as string
default:
return process.env.DATABASE_URL_PRO as string
}
}

/**
* Get dialect options for Sequelize.
* @returns {DialectOptions} The dialect options.
*/
function getDialectOptions() {
return DB_HOST_MODE === "local"
? {}
: {
ssl: {
require: true,
rejectUnauthorized: false
}
}
}

const sequelizeConnection: Sequelize = new Sequelize(getDbUri(), {
dialect: "postgres",
dialectOptions: getDialectOptions(),
logging: false
})

export default sequelizeConnection
import { config } from "dotenv"
import { Sequelize } from "sequelize"

config()
const NODE_ENV: string = process.env.NODE_ENV || "development"
const DB_HOST_MODE: string = process.env.DB_HOST_TYPE || "remote"

/**
* Get the URI for the database connection.
* @returns {string} The URI string.
*/
function getDbUri(): string {
switch (NODE_ENV) {
case "development":
return process.env.DATABASE_URL_DEV as string
case "test":
return process.env.DATABASE_URL_TEST as string
default:
return process.env.DATABASE_URL_PRO as string
}
}

/**
* Get dialect options for Sequelize.
* @returns {DialectOptions} The dialect options.
*/
function getDialectOptions() {
return DB_HOST_MODE === "local"
? {}
: {
ssl: {
require: true,
rejectUnauthorized: false
}
}
}

const sequelizeConnection: Sequelize = new Sequelize(getDbUri(), {
dialect: "postgres",
dialectOptions: getDialectOptions(),
logging: false
})

export default sequelizeConnection
4 changes: 2 additions & 2 deletions src/databases/migrations/20240520180022-create-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default {
status: {
type: new DataTypes.STRING(128),
allowNull: false,
defaultValue: true
defaultValue: "enabled"
},
createdAt: {
allowNull: false,
Expand All @@ -99,4 +99,4 @@ export default {
DROP TYPE IF EXISTS "enum_users_gender";
`);
}
};
};
8 changes: 4 additions & 4 deletions src/databases/models/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface UsersAttributes {
role?: string;
isVerified?: boolean;
is2FAEnabled?: boolean;
status?: boolean;
status?: string;
createdAt?: Date;
updatedAt?: Date;
}
Expand All @@ -42,7 +42,7 @@ class Users extends Model<UsersAttributes, UsersCreationAttributes> implements U
declare role?: string;
declare isVerified?: boolean;
declare is2FAEnabled?: boolean;
declare status?: boolean;
declare status?: string;
declare password: string;
declare createdAt?: Date;
declare updatedAt?: Date;
Expand Down Expand Up @@ -122,7 +122,7 @@ Users.init(
status: {
type: new DataTypes.STRING(128),
allowNull: true,
defaultValue: true
defaultValue: "enabled"
},
createdAt: {
field: "createdAt",
Expand Down Expand Up @@ -152,4 +152,4 @@ Users.init(
}
);

export default Users;
export default Users;
9 changes: 6 additions & 3 deletions src/databases/seeders/20240520202759-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const userOne = {
birthDate: "2-2-2014",
language: "english",
currency: "USD",
role: "buyer"
role: "buyer",
status: "enabled"
}
const userTwo = {
createdAt: new Date(),
Expand All @@ -27,7 +28,8 @@ const userTwo = {
birthDate: "1990-01-01",
language: "English",
currency: "USD",
role: "buyer"
role: "buyer",
status: "enabled"
}

const userThree = {
Expand All @@ -43,7 +45,8 @@ const userThree = {
birthDate: "2-2-2014",
language: "english",
currency: "USD",
role: "buyer"
role: "buyer",
status: "enabled"
}

const up = (queryInterface: QueryInterface) => queryInterface.bulkInsert("users",[userOne, userTwo, userThree])
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import SwaggerUi from "swagger-ui-express";
import Document from "../swagger.json";
import router from "./routes";


dotenv.config();
const app: Express = express();

Expand All @@ -16,10 +17,10 @@ app.use(express.json());
app.use(morgan(process.env.NODE_EN));
app.use(compression());
app.use(cors());
app.use("/api",router);
app.use("/api-docs", SwaggerUi.serve, SwaggerUi.setup(Document));
app.use("/api", router);

app.get("/", (req: Request, res: Response) => {
app.get("**", (req: Request, res: Response) => {
res
.status(200)
.json({ status: true, message: "Welcome to the e-Commerce Ninjas BackEnd." });
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const isUserExist = async (req: Request, res: Response, next: NextFunction) => {
if (userExists) {
return next();
}
return res.status(httpStatus.BAD_REQUEST).json({ status: httpStatus.BAD_REQUEST, message: "User not found" });
return res.status(httpStatus.NOT_FOUND).json({ status: httpStatus.NOT_FOUND, message: "User not found" });
}

return next();
Expand Down
3 changes: 2 additions & 1 deletion src/modules/auth/repository/authRepositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const findUserByAttributes = async (key:string, value:any) =>{
}

const UpdateUserByAttributes = async (updatedKey:string, updatedValue:any, whereKey:string, whereValue:any) =>{
return await Users.update({ [updatedKey]: updatedValue }, { where: { [whereKey]: whereValue} });
await Users.update({ [updatedKey]: updatedValue }, { where: { [whereKey]: whereValue} });
return await findUserByAttributes(whereKey, whereValue)
}

const createSession = async (body: any) => {
Expand Down
1 change: 0 additions & 1 deletion src/modules/auth/test/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ describe("isUserExist Middleware", () => {

afterEach(async () => {
sinon.restore();
await Users.destroy({ where: {} });
});

it("should return user already exists", (done) => {
Expand Down
14 changes: 14 additions & 0 deletions src/modules/user/controller/userControllers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Request, Response } from "express";
import httpStatus from "http-status";
import authRepositories from "../../auth/repository/authRepositories";

const updateUserStatus = async (req: Request, res: Response): Promise<void> => {
try {
const userId: number = Number(req.params.id);
const data = await authRepositories.UpdateUserByAttributes("status", req.body.status, "id", userId);
res.status(httpStatus.OK).json({ message: "Status updated successfully.", data });
} catch (error) {
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ status: httpStatus.INTERNAL_SERVER_ERROR, message: error.message });
}
};
export default { updateUserStatus };
1 change: 1 addition & 0 deletions src/modules/user/repository/userRepositories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Loading

0 comments on commit 64cf94d

Please sign in to comment.