Skip to content

Commit

Permalink
Merge pull request #12 from WanderDinizVeloso/feature/status
Browse files Browse the repository at this point in the history
Feature/status
  • Loading branch information
WanderDinizVeloso authored Dec 13, 2023
2 parents 84786eb + 0f45502 commit 8156e43
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 60 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MongooseModule } from '@nestjs/mongoose';

import { AwsModule } from './aws/aws.module';
import { OrdersModule } from './orders/orders.module';
import { StatusModule } from './status/status.module';
import { TicketsModule } from './tickets/tickets.module';

@Module({
Expand All @@ -17,6 +18,7 @@ import { TicketsModule } from './tickets/tickets.module';
OrdersModule,
TicketsModule,
AwsModule,
StatusModule,
],
controllers: [],
providers: [],
Expand Down
2 changes: 0 additions & 2 deletions src/orders/orders.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const SUMMARY_MESSAGE = {
create: 'Create order.',
findAll: 'Search all orders.',
findOne: 'Search for a order by ID.',
update: 'Update order data by ID.',
remove: 'Remove order by ID.',
};

@ApiTags('Orders')
Expand Down
2 changes: 2 additions & 0 deletions src/orders/orders.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { OrdersController } from './orders.controller';
import { OrdersService } from './orders.service';
import { Order, OrderSchema } from './schema/order.schema';
import { TicketsModule } from 'src/tickets/tickets.module';
import { StatusModule } from 'src/status/status.module';

@Module({
imports: [
MongooseModule.forFeature([{ name: Order.name, schema: OrderSchema }]),
forwardRef(() => TicketsModule),
StatusModule,
],
controllers: [OrdersController],
providers: [OrdersService],
Expand Down
27 changes: 11 additions & 16 deletions src/orders/orders.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Order, OrderDocument } from './schema/order.schema';
import { TicketsService } from 'src/tickets/tickets.service';
import { created, notFound, removed, updated } from 'src/utils/messages-response';
import { EIGHT_HOURS, TEN_SECONDS } from 'src/utils/redis-times';
import { StatusService } from 'src/status/status.service';

const ORDER = 'order';
const ORDERS = 'orders';
Expand All @@ -21,11 +22,16 @@ export class OrdersService {
@InjectModel(Order.name) private orderModel: Model<OrderDocument>,
@Inject(CACHE_MANAGER) private cacheManager: Cache,
@Inject(forwardRef(() => TicketsService)) private ticketService: TicketsService,
private statusService: StatusService,
) {}

async create(createOrderDto: CreateOrderDto): Promise<IPostReturn> {
const order = await this.orderModel.create({ ...createOrderDto, active: true });

await this.cacheManager.set(order._id, order, EIGHT_HOURS);
await Promise.all([
this.cacheManager.set(order._id, order, EIGHT_HOURS),
this.statusService.create(order._id),
]);

const ticket = await this.ticketService.create(order._id);

Expand Down Expand Up @@ -64,27 +70,16 @@ export class OrdersService {
{ new: true },
);

if (!order) {
throw new NotFoundException(notFound(ORDER));
}

await this.cacheManager.set(id, order, EIGHT_HOURS);

return { message: updated(ORDER) };
}

async remove(id: string): Promise<{ message: string }> {
const order = await this.orderModel.findOneAndUpdate(
{ _id: id, active: true },
{ active: false },
{ new: true },
);

if (!order) {
throw new NotFoundException(notFound(ORDER));
}

await this.cacheManager.del(id);
await Promise.all([
this.orderModel.findOneAndUpdate({ _id: id, active: true }, { active: false }, { new: true }),
this.cacheManager.del(id),
]);

return { message: removed(ORDER) };
}
Expand Down
15 changes: 1 addition & 14 deletions src/orders/schema/order.schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Prop, Schema, SchemaFactory, raw } from '@nestjs/mongoose';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

type OrderDocument = Order & Document;
Expand All @@ -11,19 +11,6 @@ class Order extends Document {
@Prop()
description: string;

@Prop()
preparerName: string;

@Prop(
raw({
ticketCreated: { type: Date },
preparationStarted: { type: Date },
preparationFinished: { type: Date },
customerReceived: { type: Date },
}),
)
status: Record<string, Date>;

@Prop()
active: boolean;
}
Expand Down
12 changes: 12 additions & 0 deletions src/status/interface/status.interface.ts
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 };
38 changes: 38 additions & 0 deletions src/status/schema/status.schema.ts
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 };
20 changes: 20 additions & 0 deletions src/status/status.controller.spec.ts
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();
});
});
21 changes: 21 additions & 0 deletions src/status/status.controller.ts
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();
}
}
14 changes: 14 additions & 0 deletions src/status/status.module.ts
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 {}
19 changes: 19 additions & 0 deletions src/status/status.service.spec.ts
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();
});
});
63 changes: 63 additions & 0 deletions src/status/status.service.ts
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);
}
}
8 changes: 4 additions & 4 deletions src/tickets/tickets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TicketsService } from './tickets.service';
import { IQueueTicketResponse } from './interface/ticket.interface';

const SUMMARY_MESSAGE = {
receiveTicketMessage: 'Search for a ticket in queue.',
queueReceiveMessage: 'Search for a ticket in queue.',
};

@ApiTags('Tickets')
Expand All @@ -14,8 +14,8 @@ export class TicketsController {
constructor(private readonly ticketsService: TicketsService) {}

@Get()
@ApiOperation({ summary: SUMMARY_MESSAGE.receiveTicketMessage })
receiveTicketMessage(): Promise<IQueueTicketResponse | string> {
return this.ticketsService.receiveTicketMessage();
@ApiOperation({ summary: SUMMARY_MESSAGE.queueReceiveMessage })
queueReceiveMessage(): Promise<IQueueTicketResponse | string> {
return this.ticketsService.queueReceiveMessage();
}
}
4 changes: 3 additions & 1 deletion src/tickets/tickets.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import { Module, forwardRef } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';

import { Ticket, TicketSchema } from './schema/ticket.schema';
import { AwsModule } from 'src/aws/aws.module';
import { OrdersModule } from 'src/orders/orders.module';
import { StatusModule } from 'src/status/status.module';
import { TicketsController } from './tickets.controller';
import { TicketsService } from './tickets.service';
import { AwsModule } from 'src/aws/aws.module';

@Module({
imports: [
MongooseModule.forFeature([{ name: Ticket.name, schema: TicketSchema }]),
forwardRef(() => OrdersModule),
AwsModule,
StatusModule,
],
controllers: [TicketsController],
providers: [TicketsService],
Expand Down
Loading

0 comments on commit 8156e43

Please sign in to comment.