Skip to content

Commit

Permalink
refactor: update mongo db to v4 & update faker to last lts version
Browse files Browse the repository at this point in the history
  • Loading branch information
chumarrento committed Jan 28, 2022
1 parent 22f7440 commit 6719b9f
Show file tree
Hide file tree
Showing 34 changed files with 781 additions and 626 deletions.
1,175 changes: 673 additions & 502 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
"author": "Lucas Pereira",
"license": "GPL-3.0-or-later",
"devDependencies": {
"@shelf/jest-mongodb": "^1.2.3",
"@shelf/jest-mongodb": "^2.2.0",
"@types/bcrypt": "^5.0.0",
"@types/express": "^4.17.13",
"@types/faker": "^5.1.5",
"@types/faker": "^5.5.3",
"@types/graphql": "^14.5.0",
"@types/graphql-iso-date": "^3.4.0",
"@types/jest": "^27.4.0",
"@types/jsonwebtoken": "^8.5.8",
"@types/mongodb": "^3.6.3",
"@types/node": "^17.0.12",
"@types/mongodb": "^4.0.7",
"@types/node": "^17.0.13",
"@types/supertest": "^2.0.11",
"@types/swagger-ui-express": "^4.1.3",
"@types/validator": "^13.7.1",
Expand All @@ -47,7 +47,7 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-standard": "^5.0.0",
"faker": "^5.1.0",
"faker": "^5.5.3",
"git-commit-msg-linter": "^4.0.7",
"husky": "^7.0.4",
"jest": "^27.4.7",
Expand All @@ -66,7 +66,7 @@
"graphql-iso-date": "^3.6.1",
"jsonwebtoken": "^8.5.1",
"module-alias": "^2.2.2",
"mongodb": "^3.6.2",
"mongodb": "^4.3.1",
"nodemon": "^2.0.15",
"swagger-ui-express": "^4.3.0",
"validator": "^13.7.0"
Expand Down
15 changes: 8 additions & 7 deletions src/infra/db/mongodb/account-mongo-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import {
LoadAccountByTokenRepository,
CheckAccountByEmailRepository
} from '@/data/protocols/'
import { ObjectId } from 'mongodb'

export class AccountMongoRepository implements AddAccountRepository, LoadAccountByEmailRepository, UpdateAccessTokenRepository, LoadAccountByTokenRepository, CheckAccountByEmailRepository {
async add (accountData: AddAccountRepository.Params): Promise<AddAccountRepository.Result> {
const accountCollection = await MongoHelper.getCollection('accounts')
const accountCollection = MongoHelper.getCollection('accounts')
const result = await accountCollection.insertOne(accountData)
return result.ops[0] !== null
return result.insertedId !== null
}

async loadByEmail (email: string): Promise<LoadAccountByEmailRepository.Result> {
const accountCollection = await MongoHelper.getCollection('accounts')
const accountCollection = MongoHelper.getCollection('accounts')
const account = await accountCollection.findOne({
email
}, {
Expand All @@ -29,7 +30,7 @@ export class AccountMongoRepository implements AddAccountRepository, LoadAccount
}

async checkByEmail (email: string): Promise<CheckAccountByEmailRepository.Result> {
const accountCollection = await MongoHelper.getCollection('accounts')
const accountCollection = MongoHelper.getCollection('accounts')
const account = await accountCollection.findOne({
email
}, {
Expand All @@ -41,9 +42,9 @@ export class AccountMongoRepository implements AddAccountRepository, LoadAccount
}

async updateAccessToken (id: string, token: string): Promise<void> {
const accountCollection = await MongoHelper.getCollection('accounts')
const accountCollection = MongoHelper.getCollection('accounts')
await accountCollection.updateOne({
_id: id
_id: new ObjectId(id)
}, {
$set: {
accessToken: token
Expand All @@ -52,7 +53,7 @@ export class AccountMongoRepository implements AddAccountRepository, LoadAccount
}

async loadByToken (token: string, role?: string): Promise<LoadAccountByTokenRepository.Result> {
const accountCollection = await MongoHelper.getCollection('accounts')
const accountCollection = MongoHelper.getCollection('accounts')
const account = await accountCollection.findOne({
accessToken: token,
$or: [{
Expand Down
2 changes: 1 addition & 1 deletion src/infra/db/mongodb/log-mongo-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MongoHelper } from '@/infra/db'

export class LogMongoRepository implements LogErrorRepository {
async logError (stack: string): Promise<void> {
const errorCollection = await MongoHelper.getCollection('errors')
const errorCollection = MongoHelper.getCollection('errors')
await errorCollection.insertOne({
stack,
date: new Date()
Expand Down
8 changes: 2 additions & 6 deletions src/infra/db/mongodb/mongo-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@ export const MongoHelper = {

async connect (uri: string): Promise<void> {
this.uri = uri
this.client = await MongoClient.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true
})
this.client = await MongoClient.connect(uri)
},

async disconnect (): Promise<void> {
await this.client.close()
this.client = null
},

async getCollection (name: string): Promise<Collection> {
if (!this.client?.isConnected()) await this.connect(this.uri)
getCollection (name: string): Collection {
return this.client.db().collection(name)
},

Expand Down
10 changes: 5 additions & 5 deletions src/infra/db/mongodb/survey-mongo-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { ObjectId } from 'mongodb'

export class SurveyMongoRepository implements AddSurveyRepository, LoadSurveysRepository, LoadSurveyByIdRepository, CheckSurveyByIdRepository, LoadAnswersBySurveyRepository {
async add (surveyData: AddSurveyRepository.Params): Promise<void> {
const surveyCollection = await MongoHelper.getCollection('surveys')
const surveyCollection = MongoHelper.getCollection('surveys')
await surveyCollection.insertOne(surveyData)
}

async loadAll (accountId: string): Promise<LoadSurveysRepository.Result> {
const surveyCollection = await MongoHelper.getCollection('surveys')
const surveyCollection = MongoHelper.getCollection('surveys')

const query = new QueryBuilder()
.lookup({
Expand Down Expand Up @@ -44,13 +44,13 @@ export class SurveyMongoRepository implements AddSurveyRepository, LoadSurveysRe
}

async loadById (id: string): Promise<LoadSurveyByIdRepository.Result> {
const surveyCollection = await MongoHelper.getCollection('surveys')
const surveyCollection = MongoHelper.getCollection('surveys')
const survey = await surveyCollection.findOne({ _id: new ObjectId(id) })
return survey && MongoHelper.map(survey)
}

async loadAnswers (id: string): Promise<LoadAnswersBySurveyRepository.Result> {
const surveyCollection = await MongoHelper.getCollection('surveys')
const surveyCollection = MongoHelper.getCollection('surveys')
const query = new QueryBuilder()
.match({
_id: new ObjectId(id)
Expand All @@ -65,7 +65,7 @@ export class SurveyMongoRepository implements AddSurveyRepository, LoadSurveysRe
}

async checkById (id: string): Promise<CheckSurveyByIdRepository.Result> {
const surveyCollection = await MongoHelper.getCollection('surveys')
const surveyCollection = MongoHelper.getCollection('surveys')
const survey = await surveyCollection.findOne({
_id: new ObjectId(id)
}, {
Expand Down
7 changes: 4 additions & 3 deletions src/infra/db/mongodb/survey-result-mongo-repository.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { LoadSurveyResultRepository, SaveSurveyResultRepository } from '@/data/protocols'
import { SurveyResultModel } from '@/domain/models'
import { MongoHelper, QueryBuilder } from '@/infra/db'
import { ObjectId } from 'mongodb'

export class SurveyResultMongoRepository implements SaveSurveyResultRepository, LoadSurveyResultRepository {
async save (data: SaveSurveyResultRepository.Params): Promise<void> {
const surveyResultCollection = await MongoHelper.getCollection('surveyResults')
const surveyResultCollection = MongoHelper.getCollection('surveyResults')
await surveyResultCollection.findOneAndUpdate({
surveyId: new ObjectId(data.surveyId),
accountId: new ObjectId(data.accountId)
Expand All @@ -19,7 +20,7 @@ export class SurveyResultMongoRepository implements SaveSurveyResultRepository,
}

async loadBySurveyId (surveyId: string, accountId: string): Promise<LoadSurveyResultRepository.Result> {
const surveyResultCollection = await MongoHelper.getCollection('surveyResults')
const surveyResultCollection = MongoHelper.getCollection('surveyResults')
const query = new QueryBuilder()
.match({
surveyId: new ObjectId(surveyId)
Expand Down Expand Up @@ -188,7 +189,7 @@ export class SurveyResultMongoRepository implements SaveSurveyResultRepository,
answers: '$answers'
})
.build()
const surveyResult = await surveyResultCollection.aggregate(query).toArray()
const surveyResult = await surveyResultCollection.aggregate<SurveyResultModel>(query).toArray()
return surveyResult.length ? surveyResult[0] : null
}
}
4 changes: 2 additions & 2 deletions tests/data/mocks/mock-cryptography.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Hasher, Decrypter, Encrypter, HashComparer } from '@/data/protocols'
import faker from 'faker'

export class HasherSpy implements Hasher {
result = faker.random.uuid()
result = faker.datatype.uuid()
plaintext: string

async hash (plaintext: string): Promise<string> {
Expand Down Expand Up @@ -34,7 +34,7 @@ export class HashComparerSpy implements HashComparer {
}

export class EncrypterSpy implements Encrypter {
result = faker.random.uuid()
result = faker.datatype.uuid()
plaintext: string

async encrypt (plaintext: string): Promise<string> {
Expand Down
4 changes: 2 additions & 2 deletions tests/data/mocks/mock-db-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class AddAccountRepositorySpy implements AddAccountRepository {

export class LoadAccountByEmailRepositorySpy implements LoadAccountByEmailRepository {
result = {
id: faker.random.uuid(),
id: faker.datatype.uuid(),
name: faker.name.findName(),
password: faker.internet.password()
}
Expand All @@ -39,7 +39,7 @@ export class CheckAccountByEmailRepositorySpy implements CheckAccountByEmailRepo
}

export class LoadAccountByTokenRepositorySpy implements LoadAccountByTokenRepository {
result = { id: faker.random.uuid() }
result = { id: faker.datatype.uuid() }
token: string
role: string

Expand Down
2 changes: 1 addition & 1 deletion tests/data/usecases/db-check-survey-by-id.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let surveyId: string

describe('DbCheckSurveyById', () => {
beforeEach(() => {
surveyId = faker.random.uuid()
surveyId = faker.datatype.uuid()
})

test('Should call CheckSurveyByIdRepository with correct id', async () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/data/usecases/db-load-account-by-token.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let role: string

describe('DbLoadAccountByToken Usecase', () => {
beforeEach(() => {
token = faker.random.uuid()
token = faker.datatype.uuid()
role = faker.random.word()
})

Expand Down
2 changes: 1 addition & 1 deletion tests/data/usecases/db-load-answers-by-survey.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('DbLoadSurveyById', () => {
})

beforeEach(() => {
surveyId = faker.random.uuid()
surveyId = faker.datatype.uuid()
})

test('Should call LoadSurveyByIdRepository with correct id', async () => {
Expand Down
4 changes: 2 additions & 2 deletions tests/data/usecases/db-load-survey-result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ describe('DbLoadSurveyResult UseCase', () => {
})

beforeEach(() => {
surveyId = faker.random.uuid()
accountId = faker.random.uuid()
surveyId = faker.datatype.uuid()
accountId = faker.datatype.uuid()
})

test('Should call LoadSurveyResultRepository with correct values', async () => {
Expand Down
6 changes: 3 additions & 3 deletions tests/data/usecases/db-load-surveys.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ describe('DbLoadSurveys Usecase', () => {

test('Should call LoadSurveysRepository', async () => {
const { sut, loadSurveysRepositorySpy } = makeSut()
const accountId = faker.random.uuid()
const accountId = faker.datatype.uuid()
await sut.load(accountId)
expect(loadSurveysRepositorySpy.accountId).toBe(accountId)
})

test('Should return a list of Surveys on succes', async () => {
const { sut, loadSurveysRepositorySpy } = makeSut()
const surveys = await sut.load(faker.random.uuid())
const surveys = await sut.load(faker.datatype.uuid())
expect(surveys).toEqual(loadSurveysRepositorySpy.result)
})

test('Should throws if LoadSurveysRepository throws', async () => {
const { sut, loadSurveysRepositorySpy } = makeSut()
jest.spyOn(loadSurveysRepositorySpy, 'loadAll').mockRejectedValueOnce(new Error())
const promise = sut.load(faker.random.uuid())
const promise = sut.load(faker.datatype.uuid())
await expect(promise).rejects.toThrow()
})
})
8 changes: 4 additions & 4 deletions tests/domain/mocks/mock-survey-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { SaveSurveyResult } from '@/domain/usecases'
import faker from 'faker'

export const mockSaveSurveyResultParams = (): SaveSurveyResult.Params => ({
accountId: faker.random.uuid(),
surveyId: faker.random.uuid(),
accountId: faker.datatype.uuid(),
surveyId: faker.datatype.uuid(),
answer: faker.random.word(),
date: faker.date.recent()
})

export const mockSurveyResultModel = (): SurveyResultModel => ({
surveyId: faker.random.uuid(),
surveyId: faker.datatype.uuid(),
question: faker.random.words(),
answers: [{
answer: faker.random.word(),
Expand All @@ -28,7 +28,7 @@ export const mockSurveyResultModel = (): SurveyResultModel => ({
})

export const mockEmptySurveyResultModel = (): SurveyResultModel => ({
surveyId: faker.random.uuid(),
surveyId: faker.datatype.uuid(),
question: faker.random.words(),
answers: [{
answer: faker.random.word(),
Expand Down
2 changes: 1 addition & 1 deletion tests/domain/mocks/mock-survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const mockAddSurveyParams = (): AddSurvey.Params => ({
})

export const mockSurveyModel = (): SurveyModel => Object.assign(mockAddSurveyParams(), {
id: faker.random.uuid()
id: faker.datatype.uuid()
})

export const mockSurveysList = (): SurveyModel[] => [
Expand Down
15 changes: 9 additions & 6 deletions tests/infra/db/mongodb/account-mongo-repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('Account Mongo Repository', () => {
})

beforeEach(async () => {
accountCollection = await MongoHelper.getCollection('accounts')
accountCollection = MongoHelper.getCollection('accounts')
await accountCollection.deleteMany({})
})
describe('add()', () => {
Expand Down Expand Up @@ -70,11 +70,14 @@ describe('Account Mongo Repository', () => {
test('Should update the account accessToken on success', async () => {
const sut = makeSut()
const res = await accountCollection.insertOne(mockAddAccountParams())
const fakeAccount = res.ops[0]
const fakeAccount = await accountCollection.findOne({ _id: res.insertedId })

expect(fakeAccount.accessToken).toBeFalsy()
const accessToken = faker.random.uuid()
await sut.updateAccessToken(fakeAccount._id, accessToken)

const accessToken = faker.datatype.uuid()
await sut.updateAccessToken(fakeAccount._id.toHexString(), accessToken)
const account = await accountCollection.findOne({ _id: fakeAccount._id })

expect(account).toBeTruthy()
expect(account.accessToken).toBe(accessToken)
})
Expand All @@ -84,13 +87,13 @@ describe('Account Mongo Repository', () => {
let name = faker.name.findName()
let email = faker.internet.email()
let password = faker.internet.password()
let accessToken = faker.random.uuid()
let accessToken = faker.datatype.uuid()

beforeEach(() => {
name = faker.name.findName()
email = faker.internet.email()
password = faker.internet.password()
accessToken = faker.random.uuid()
accessToken = faker.datatype.uuid()
})

test('Should return an account on loadByToken without role', async () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/infra/db/mongodb/log-mongo-repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Account Mongo Repository', () => {
})

beforeEach(async () => {
errorCollection = await MongoHelper.getCollection('errors')
errorCollection = MongoHelper.getCollection('errors')
await errorCollection.deleteMany({})
})

Expand Down
18 changes: 0 additions & 18 deletions tests/infra/db/mongodb/mongo-helper.spec.ts

This file was deleted.

Loading

0 comments on commit 6719b9f

Please sign in to comment.