Skip to content
Merged
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
51 changes: 51 additions & 0 deletions src/auth/application/messages/password-change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Message } from 'src/common/application/events/message/message';
import { ISession } from '../model/session.interface';

export class AccountRegistered extends Message {
serialize(): string {
let data= {
id: this.id,
password: this.password
}
return JSON.stringify(data)
}
static create(
sessions: ISession[],
id: string,
email: string,
password: string,
created_at: Date,
isConfirmed:boolean,
code: null,
code_created_at:null,
idUser:string,
idStripe:string
){
return new AccountRegistered(
sessions,
id,
email,
password,
created_at,
isConfirmed,
code,
code_created_at,
idUser,
idStripe
)
}
constructor(
public sessions: ISession[],
public id: string,
public email: string,
public password: string,
public created_at: Date,
public isConfirmed:boolean,
public code: null,
public code_created_at:null,
public idUser:string,
public idStripe:string
){
super()
}
}
24 changes: 11 additions & 13 deletions src/auth/application/messages/session-registered.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
import { Message } from 'src/common/application/events/message/message';
import { ISession } from '../model/session.interface';

export class SessionRegistered extends Message {
serialize(): string {
let data= {
id: this.id,
expired_at: this.expired_at,
push_token: this.push_token,
accountId:this.accountId
session:{
id: this.id,
expired_at: this.session.expired_at,
push_token: this.session.push_token,
accountId:this.session.accountId
}
}
return JSON.stringify(data)
}
static create(
id: string,
expired_at: Date,
push_token: string,
accountId:string
id:string,
session:ISession
){
return new SessionRegistered(
id,
expired_at,
push_token,
accountId
session
)
}
constructor(
public id: string,
public expired_at: Date,
public push_token: string,
public accountId:string
public session:ISession
){
super()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { IQueryUserRepository } from "src/user/application/repository/user.query
import { UserId } from "src/user/domain/value-object/user-id";
import { ErrorRegisteringSessionApplicationException } from "../../application-exception/error-registering-session-application-exception";
import { AccountNotFoundApplicationException } from "../../application-exception/account-not-found-application-exception";
import { IMessagesPublisher } from "src/common/application/messages/messages-publisher/messages-publisher.interface";
import { SessionRegistered } from "../../messages/session-registered";

export class LogInUserApplicationService extends IApplicationService
<LogInUserApplicationRequestDTO,LogInUserApplicationResponseDTO> {
Expand All @@ -27,7 +29,8 @@ export class LogInUserApplicationService extends IApplicationService
private readonly encryptor: IEncryptor,
private readonly idGen:IIdGen<string>,
private readonly jwtGen:IJwtGenerator<string>,
private readonly dateHandler:IDateHandler
private readonly dateHandler:IDateHandler,
private readonly messagePublisher:IMessagesPublisher
){
super()
}
Expand Down Expand Up @@ -56,18 +59,26 @@ export class LogInUserApplicationService extends IApplicationService
const idSession = await this.idGen.genId()

const jwt = this.jwtGen.generateJwt( idSession )

let sessionResponse=await this.commandTokenSessionRepository.createSession(
{

const session={
expired_at: this.dateHandler.getExpiry(),
id: idSession,
push_token: null,
accountId: account.id
})
}

let sessionResponse=await this.commandTokenSessionRepository.createSession(session)

if (!sessionResponse.isSuccess())
return Result.fail(new ErrorRegisteringSessionApplicationException())

this.messagePublisher.publish([
SessionRegistered.create(
account.id,
session
)
])

return Result.success({
id: user.getId().Value,
email: account.email,
Expand Down
20 changes: 18 additions & 2 deletions src/auth/infraestructure/controller/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ import { AuthQueues } from "../queues/auth.queues"
import { IAccountRegistered } from "../interface/account-registered.interface"
import { AccountRegisteredSyncroniceService } from "../services/syncronice/account-registered-syncronice.service"
import mongoose, { Mongoose } from "mongoose"
import { IAccountLogIn } from "../interface/account-log-in.interface"
import { AccountLogInSyncroniceService } from "../services/syncronice/account-log-in-syncronice.service"

@ApiTags('Auth')
@Controller('auth')
Expand Down Expand Up @@ -136,14 +138,27 @@ export class AuthController {
this.syncAccountRegistered(data)
return
}
)
)

this.messageSuscriber.consume<IAccountLogIn>(
{ name: 'Messages/SessionRegistered' },
(data):Promise<void>=>{
this.syncAccountLogIn(data)
return
}
)
}

async syncAccountRegistered(data:IAccountRegistered){
let service = new AccountRegisteredSyncroniceService(mongoose)
await service.execute(data)
}

async syncAccountLogIn(data:IAccountLogIn){
let service = new AccountLogInSyncroniceService(mongoose)
await service.execute({...data})
}

@Post('register')
@ApiResponse({
status: 200,
Expand Down Expand Up @@ -197,7 +212,8 @@ export class AuthController {
this.encryptor,
this.idGen,
this.jwtGen,
this.dateHandler
this.dateHandler,
this.messagePubliser
), new NestTimer(), new NestLogger(new Logger())
// ),new NestLogger(new Logger())
),this.auditRepository,this.dateHandler
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface IAccountLogIn {
id:string
session: {
id: string;
expired_at: Date;
push_token: string;
accountId: string;
}
}
1 change: 1 addition & 0 deletions src/auth/infraestructure/queues/auth.queues.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const AuthQueues = [
{ name: 'Messages/AccountRegistered', pattern: 'AccountRegistered' },
{ name: 'Messages/SessionRegistered', pattern: 'SessionRegistered' },
]
Original file line number Diff line number Diff line change
@@ -1,30 +1,82 @@
import { IQueryAccountRepository } from "src/auth/application/repository/query-account-repository.interface"
import { OrmAccountEntity } from "../../account/orm-entities/orm-account-entity"
import { DataSource, Repository } from "typeorm"
import { IAccount } from "src/auth/application/model/account.interface"
import { Result } from "src/common/utils/result-handler/result"
import { NotFoundException } from "src/common/infraestructure/infraestructure-exception"
import { OdmAccount, OdmAccountSchema } from "../../account/odm-entities/odm-account-entity"
import { Model, Mongoose } from "mongoose"



export class OdmAccountQueryRepository implements IQueryAccountRepository<IAccount>{

constructor(dataSource:DataSource){}

findAccountByEmail(email: string): Promise<Result<IAccount>> {
throw new Error("Method not implemented.")
private readonly model: Model<OdmAccount>;

constructor( mongoose: Mongoose ) {
this.model = mongoose.model<OdmAccount>('odmaccount', OdmAccountSchema)
}

async findAccountByEmail(email: string): Promise<Result<IAccount>> {
try{
let odm=await this.model.findOne({email})

if(!odm)
return Result.fail( new NotFoundException('Find account by email unsucssessfully'))

return Result.success(odm)
}
catch(e){
return Result.fail( new NotFoundException('Find account by email unsucssessfully'))
}
}
findAccountById(id: string): Promise<Result<IAccount>> {
throw new Error("Method not implemented.")
async findAccountById(id: string): Promise<Result<IAccount>> {
try{
let odm=await this.model.findOne({id})

if(!odm)
return Result.fail( new NotFoundException('Find account by id unsucssessfully'))

return Result.success(odm)
}
catch(e){
return Result.fail( new NotFoundException('Find account by id unsucssessfully'))
}
}
findAccountByUserId(userId: string): Promise<Result<IAccount>> {
throw new Error("Method not implemented.")
async findAccountByUserId(userId: string): Promise<Result<IAccount>> {
try{
let odm=await this.model.findOne({idUser:userId})

if(!odm)
return Result.fail( new NotFoundException('Find account by user id unsucssessfully'))

return Result.success(odm)
}
catch(e){
return Result.fail( new NotFoundException('Find account by user id unsucssessfully'))
}
}
verifyAccountExistanceByEmail(email: string): Promise<Result<boolean>> {
throw new Error("Method not implemented.")
async verifyAccountExistanceByEmail(email: string): Promise<Result<boolean>> {
try{
let odm=await this.model.findOne({email})
if(!odm)
return Result.success(false)
return Result.success(true)
}
catch(e){
return Result.fail( new NotFoundException('Verify account by email unsucssessfully'))
}
}
findAllEmails(): Promise<Result<string[]>> {
throw new Error("Method not implemented.")
async findAllEmails(): Promise<Result<string[]>> {
try{
let odm=await this.model.find()
if(!odm)
return Result.success(odm
? odm.map(o=>o.email)
: []
)
}
catch(e){
return Result.fail( new NotFoundException('Find all emails unsucssessfully'))
}
}

}
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
import { Model, Mongoose } from "mongoose";
import { ISession } from "src/auth/application/model/session.interface"
import { IQueryTokenSessionRepository } from "src/auth/application/repository/query-token-session-repository.interface"
import { Result } from "src/common/utils/result-handler/result"
import { UserId } from "src/user/domain/value-object/user-id"
import { OdmAccount, OdmAccountSchema } from "../../account/odm-entities/odm-account-entity";
import { NotFoundException } from "src/common/infraestructure/infraestructure-exception";


export class OrmTokenQueryRepository implements IQueryTokenSessionRepository<ISession>{

private readonly model: Model<OdmAccount>;

constructor( mongoose: Mongoose ) {
this.model = mongoose.model<OdmAccount>('odmaccount', OdmAccountSchema)
}

findAllLastTokenSessions(): Promise<Result<string[]>> {
throw new Error("Method not implemented.")
}

findSessionById(id: string): Promise<Result<ISession>> {
throw new Error("Method not implemented.")
async findSessionById(id: string): Promise<Result<ISession>> {
try{
//TODO
let odm=await this.model.find({"sessions.id":id})
}
catch(e){
return Result.fail( new NotFoundException('Find session unsucssessfully'))
}
}
findAllTokenSessions(): Promise<Result<string[]>> {
throw new Error("Method not implemented.")
}
findSessionLastSessionByUserId(id: UserId): Promise<Result<ISession>> {
throw new Error("Method not implemented.")
async findSessionLastSessionByUserId(id: UserId): Promise<Result<ISession>> {
try{
let odm=await this.model.find({idUser:id.Value})
}
catch(e){
return Result.fail( new NotFoundException('Find all emails unsucssessfully'))
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ export class OrmTokenQueryRepository extends Repository<OrmSessionEntity> implem
try{
const sessions = await this.createQueryBuilder("session")
.select("session.push_token")
.orderBy("session.created_at", "DESC")
.orderBy("session.expired_at", "DESC")
.limit(1)
.getMany()
const tokens = sessions.map(session => session.push_token)
return Result.success(tokens)
return Result.success(sessions
? sessions.map(session => session.push_token)
: []
)
}catch(e){
return Result.fail( new NotFoundException('Error finding all emails'))
return Result.fail( new NotFoundException('Error finding all tokens'))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export interface AccountLogInInfraestructureRequestDTO {
sessions: {
id: string
expired_at: Date
push_token: null
accountId:string
id:string
session: {
id: string;
expired_at: Date;
push_token: string;
accountId: string;
}
}
Loading
Loading