-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from WanderDinizVeloso/feature/status
Feature/status
- Loading branch information
Showing
15 changed files
with
244 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
interface IStatusPayload { | ||
ticketCreatedAt: Date; | ||
sendQueueMessageAt: Date; | ||
receivedQueueMessageAt: Date; | ||
manufacturingStartedAt: Date; | ||
manufacturingFinishedAt: Date; | ||
customerReceivedAt: Date; | ||
} | ||
|
||
interface IStatusUpdatePayload extends Partial<IStatusPayload> {} | ||
|
||
export { IStatusUpdatePayload }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
import { Document } from 'mongoose'; | ||
|
||
type StatusDocument = Status & Document; | ||
|
||
@Schema({ timestamps: true, versionKey: false }) | ||
class Status extends Document { | ||
@Prop({ unique: true }) | ||
orderId: string; | ||
|
||
@Prop() | ||
orderCreatedAt: Date; | ||
|
||
@Prop() | ||
ticketCreatedAt: Date; | ||
|
||
@Prop() | ||
sendQueueMessageAt: Date; | ||
|
||
@Prop() | ||
receivedQueueMessageAt: Date; | ||
|
||
@Prop() | ||
manufacturingStartedAt: Date; | ||
|
||
@Prop() | ||
manufacturingFinishedAt: Date; | ||
|
||
@Prop() | ||
customerReceivedAt: Date; | ||
|
||
@Prop() | ||
active: boolean; | ||
} | ||
|
||
const StatusSchema = SchemaFactory.createForClass(Status); | ||
|
||
export { StatusDocument, Status, StatusSchema }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { StatusController } from './status.controller'; | ||
import { StatusService } from './status.service'; | ||
|
||
describe('RicketsController', () => { | ||
let controller: StatusController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [StatusService], | ||
providers: [StatusService], | ||
}).compile(); | ||
|
||
controller = module.get<StatusController>(StatusController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { ApiOperation, ApiTags } from '@nestjs/swagger'; | ||
|
||
import { StatusService } from './status.service'; | ||
import { Status } from './schema/status.schema'; | ||
|
||
const SUMMARY_MESSAGE = { | ||
findAll: 'Search all status.', | ||
}; | ||
|
||
@ApiTags('Status') | ||
@Controller('status') | ||
export class StatusController { | ||
constructor(private readonly statusService: StatusService) {} | ||
|
||
@Get() | ||
@ApiOperation({ summary: SUMMARY_MESSAGE.findAll }) | ||
findAll(): Promise<Status[]> { | ||
return this.statusService.findAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
|
||
import { Status, StatusSchema } from './schema/status.schema'; | ||
import { StatusController } from './status.controller'; | ||
import { StatusService } from './status.service'; | ||
|
||
@Module({ | ||
imports: [MongooseModule.forFeature([{ name: Status.name, schema: StatusSchema }])], | ||
controllers: [StatusController], | ||
providers: [StatusService], | ||
exports: [StatusService], | ||
}) | ||
export class StatusModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
|
||
import { StatusService } from './status.service'; | ||
|
||
describe('StatusService', () => { | ||
let service: StatusService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [StatusService], | ||
}).compile(); | ||
|
||
service = module.get<StatusService>(StatusService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Cache } from 'cache-manager'; | ||
import { CACHE_MANAGER } from '@nestjs/cache-manager'; | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { Model } from 'mongoose'; | ||
|
||
import { Status, StatusDocument } from './schema/status.schema'; | ||
import { IStatusUpdatePayload } from './interface/status.interface'; | ||
import { EIGHT_HOURS, TEN_SECONDS } from 'src/utils/redis-times'; | ||
|
||
const STATUS = 'status'; | ||
|
||
@Injectable() | ||
export class StatusService { | ||
constructor( | ||
@InjectModel(Status.name) private statusModel: Model<StatusDocument>, | ||
@Inject(CACHE_MANAGER) private cacheManager: Cache, | ||
) {} | ||
|
||
async create(orderId: string): Promise<void> { | ||
const status = await this.statusModel.create({ | ||
orderId, | ||
orderCreatedAt: new Date(), | ||
active: true, | ||
}); | ||
|
||
await this.cacheManager.set(status._id, status, EIGHT_HOURS); | ||
} | ||
|
||
async findAll(): Promise<Status[]> { | ||
const statusCache: Status[] = await this.cacheManager.get(STATUS); | ||
|
||
if (!statusCache) { | ||
const status = await this.statusModel.find(); | ||
|
||
await this.cacheManager.set(STATUS, status, TEN_SECONDS); | ||
|
||
return status; | ||
} | ||
|
||
return statusCache; | ||
} | ||
|
||
async update(orderId: string, statusUpdatePayload: IStatusUpdatePayload): Promise<void> { | ||
const status = await this.statusModel.findOneAndUpdate( | ||
{ orderId, active: true }, | ||
statusUpdatePayload, | ||
{ new: true }, | ||
); | ||
|
||
await this.cacheManager.set(status._id, status, EIGHT_HOURS); | ||
} | ||
|
||
async remove(orderId: string): Promise<void> { | ||
const status = await this.statusModel.findOneAndUpdate( | ||
{ orderId, active: true }, | ||
{ active: false }, | ||
{ new: true }, | ||
); | ||
|
||
await this.cacheManager.del(status._id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.