Skip to content
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
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*.js
node_modules
dist
70 changes: 70 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"env": {
"es2021": true,
"node": true,
"jest": true
},
"extends": [
"airbnb-base",
"plugin:@typescript-eslint/recommended",
"prettier",
"prettier:prettier/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint",
"eslint-plugin-import-helpers",
"prettier"
],
"rules": {
"prettier/prettier": "error",
"camelcase": "off",
"import/no-unresolved": "error",
"@typescript-eslint/naming-convention": [
"error",
{
"selector": "interface",
"format": ["PascalCase"],
"custom": {
"regex": "^I[A-Z]",
"match": true
}
}
],
"class-methods-use-this": "off",
"import/prefer-default-export": "off",
"no-shadow": "off",
"no-console": "off",
"no-useless-constructor": "off",
"no-empty-function": "off",
"lines-between-class-members": "off",
"import/extensions": [
"error",
"ignorePackages",
{
"ts": "never"
}
],
"import-helpers/order-imports": [
"warn",
{
"newlinesBetween": "always",
"groups": ["module", "/^@shared/", ["parent", "sibling", "index"]],
"alphabetize": { "order": "asc", "ignoreCase": true }
}
],
"import/no-extraneous-dependencies": [
"error",
{ "devDependencies": ["**/*.spec.js"] }
]
},
"settings": {
"import/resolver": {
"typescript": {}
}
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
90 changes: 0 additions & 90 deletions README.md

This file was deleted.

40 changes: 40 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "teste-back-end",
"version": "1.0.0",
"main": "index.js",
"repository": "[email protected]:antoniowellington542/teste-back-end.git",
"author": "antoniowellington542 <[email protected]>",
"license": "MIT",
"scripts": {
"dev": "ts-node-dev --transpile-only --ignore-watch node_modules --respawn ./src/server.ts"
},
"dependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/mongoose": "^5.11.97",
"bcryptjs": "^2.4.3",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"express-async-errors": "^3.1.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^6.4.4",
"reflect-metadata": "^0.1.13",
"tsyringe": "^4.7.0"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/jsonwebtoken": "^8.5.8",
"@types/reflect-metadata": "^0.1.0",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"eslint": "^8.19.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-typescript": "^3.2.4",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-import-helpers": "^1.2.1",
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.7.1",
"ts-node-dev": "^2.0.0",
"typescript": "^4.7.4"
}
}
3 changes: 3 additions & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
singleQuote: true,
}
7 changes: 7 additions & 0 deletions src/@types/express/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare namespace Express {
export interface Request {
user: {
id: string;
}
}
}
16 changes: 16 additions & 0 deletions src/database/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import mongoose from 'mongoose';
import 'dotenv/config';

const password = encodeURI(process.env.DB_PASSWORD);

function createConnection(): void {
try {
mongoose.connect(
`mongodb+srv://${process.env.DB_USERNAME}:${password}@${process.env.DB_NAME}.aupfh.mongodb.net/?retryWrites=true&w=majority`);
console.log('Conectado')
} catch (err) {
console.log(err);
}
}

export { createConnection };
9 changes: 9 additions & 0 deletions src/errors/AppError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class AppError {
public readonly message: string;
public readonly statusCode: number;

constructor(message: string, statusCode = 400) {
this.message = message;
this.statusCode = statusCode;
}
}
43 changes: 43 additions & 0 deletions src/middlewares/ensureAuthenticated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { NextFunction, Request, Response } from "express";
import { verify } from "jsonwebtoken";
import 'dotenv/config';
import { container } from "tsyringe";
import { VolunteersRepository } from "../modules/volunteers/repositories/VolunteersRepository";
import { AppError } from "../errors/AppError";
import mongoose, { Mongoose, Schema, SchemaType, SchemaTypes } from "mongoose";

interface IPayload {
sub: string;
}

interface IRequestVoluntary {
id: string;
}

export async function ensureAuthenticated(request: Request, response: Response, next: NextFunction) {
const authHeader = request.headers.authorization;

if(!authHeader) {
throw new Error('Token missing!');
}

const [, token] = authHeader.split(' ');

try {
const { sub: user_id } = verify(token, process.env.SECRET) as IPayload;
const volunteersRepository = container.resolve(VolunteersRepository);
const user = await volunteersRepository.findById(user_id);

if (!user) {
throw new AppError('User not Found!', 403);
}

request.user = {
id: user_id
}

next()
} catch {
throw new Error('Invalid Token!');
}
}
20 changes: 20 additions & 0 deletions src/modules/volunteers/dtos/ICreateVoluntaryDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { IVoluntary } from "../interfaces/IVoluntary";

interface ICreateVoluntaryDTO {
email: IVoluntary['email'];
name: IVoluntary['name'];
password: IVoluntary['password'];
birthdate: IVoluntary['birthdate'];
cellphone: IVoluntary['cellphone'];
occupation: IVoluntary['occupation'];
university: IVoluntary['university'];
semester: IVoluntary['semester'];
speciality: IVoluntary['speciality'];
listFreeDaysOfWeek: IVoluntary['listFreeDaysOfWeek'];
numberOfFreeDaysOfWeek: IVoluntary['numberOfFreeDaysOfWeek'];
timeOfExperience: IVoluntary['timeOfExperience'];
howMuchParticipate: IVoluntary['howMuchParticipate'];
howDidKnowOfSDR: IVoluntary['howDidKnowOfSDR'];
}

export { ICreateVoluntaryDTO };
20 changes: 20 additions & 0 deletions src/modules/volunteers/dtos/IUpdadeVoluntaryDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { IVoluntary } from "../interfaces/IVoluntary";

interface IUpdateVoluntaryDTO {
email: IVoluntary['email'];
name: IVoluntary['name'];
password: IVoluntary['password'];
birthdate: IVoluntary['birthdate'];
cellphone: IVoluntary['cellphone'];
occupation: IVoluntary['occupation'];
university: IVoluntary['university'];
semester: IVoluntary['semester'];
speciality: IVoluntary['speciality'];
listFreeDaysOfWeek: IVoluntary['listFreeDaysOfWeek'];
numberOfFreeDaysOfWeek: IVoluntary['numberOfFreeDaysOfWeek'];
timeOfExperience: IVoluntary['timeOfExperience'];
howMuchParticipate: IVoluntary['howMuchParticipate'];
howDidKnowOfSDR: IVoluntary['howDidKnowOfSDR'];
}

export { IUpdateVoluntaryDTO };
21 changes: 21 additions & 0 deletions src/modules/volunteers/dtos/IVolunteersDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IVoluntary } from "../interfaces/IVoluntary";

interface IVolunteersDTO {

email: IVoluntary['email'];
name: IVoluntary['name'];
password: IVoluntary['password'];
birthdate: IVoluntary['birthdate'];
cellphone: IVoluntary['cellphone'];
occupation: IVoluntary['occupation'];
university: IVoluntary['university'];
semester: IVoluntary['semester'];
speciality: IVoluntary['speciality'];
listFreeDaysOfWeek: IVoluntary['listFreeDaysOfWeek'];
numberOfFreeDaysOfWeek: IVoluntary['numberOfFreeDaysOfWeek'];
timeOfExperience: IVoluntary['timeOfExperience'];
howMuchParticipate: IVoluntary['howMuchParticipate'];
howDidKnowOfSDR: IVoluntary['howDidKnowOfSDR'];
}

export { IVolunteersDTO };
Loading