-
Notifications
You must be signed in to change notification settings - Fork 0
[SSF-129] add tracking link and shipping cost to order #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
874c654
700b124
e23efe2
a97afbb
bbf6f45
7075104
dee5203
7c82a53
f8d23f3
28c04f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
|
||
| export class UpdateOrderEntity1769990652833 implements MigrationInterface { | ||
| public async up(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query(` | ||
| ALTER TABLE orders | ||
| ADD COLUMN IF NOT EXISTS tracking_link VARCHAR(255), | ||
| ADD COLUMN IF NOT EXISTS shipping_cost NUMERIC(10,2); | ||
|
|
||
| UPDATE orders | ||
| SET tracking_link = 'www.samplelink/samplelink', | ||
| shipping_cost = 20.00 | ||
| WHERE status = 'delivered' OR status = 'shipped' AND shipped_at IS NOT NULL; | ||
| `); | ||
| } | ||
|
|
||
| public async down(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query(` | ||
| ALTER TABLE orders | ||
| DROP COLUMN IF EXISTS tracking_link, | ||
| DROP COLUMN IF EXISTS shipping_cost; | ||
| `); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { IsUrl, IsNumber, Min, IsOptional } from 'class-validator'; | ||
|
|
||
| export class TrackingCostDto { | ||
| @IsUrl({}, { message: 'Tracking link must be a valid URL' }) | ||
| @IsOptional() | ||
| trackingLink?: string; | ||
|
|
||
| @IsNumber( | ||
| { maxDecimalPlaces: 2 }, | ||
| { message: 'Shipping cost must have at most 2 decimal places' }, | ||
| ) | ||
| @Min(0, { message: 'Shipping cost cannot be negative' }) | ||
| @IsOptional() | ||
| shippingCost?: number; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| import { Injectable, NotFoundException } from '@nestjs/common'; | ||
| import { | ||
| BadRequestException, | ||
| Injectable, | ||
| NotFoundException, | ||
| } from '@nestjs/common'; | ||
| import { InjectRepository } from '@nestjs/typeorm'; | ||
| import { Repository, In } from 'typeorm'; | ||
| import { Order } from './order.entity'; | ||
|
|
@@ -7,6 +11,7 @@ import { FoodManufacturer } from '../foodManufacturers/manufacturer.entity'; | |
| import { FoodRequest } from '../foodRequests/request.entity'; | ||
| import { validateId } from '../utils/validation.utils'; | ||
| import { OrderStatus } from './types'; | ||
| import { TrackingCostDto } from './dtos/tracking-cost.dto'; | ||
|
|
||
| @Injectable() | ||
| export class OrdersService { | ||
|
|
@@ -124,6 +129,11 @@ export class OrdersService { | |
| if (!order) { | ||
| throw new NotFoundException(`Order ${orderId} not found`); | ||
| } | ||
| if (!order.foodManufacturer) { | ||
| throw new NotFoundException( | ||
| `Order ${orderId} does not have a food manufacturer assigned`, | ||
| ); | ||
| } | ||
| return order.foodManufacturer; | ||
| } | ||
|
|
||
|
|
@@ -137,8 +147,9 @@ export class OrdersService { | |
| .set({ | ||
| status: newStatus as OrderStatus, | ||
| shippedBy: 1, | ||
| shippedAt: newStatus === OrderStatus.SHIPPED ? new Date() : null, | ||
| deliveredAt: newStatus === OrderStatus.DELIVERED ? new Date() : null, | ||
| shippedAt: newStatus === OrderStatus.SHIPPED ? new Date() : undefined, | ||
| deliveredAt: | ||
| newStatus === OrderStatus.DELIVERED ? new Date() : undefined, | ||
| }) | ||
| .where('order_id = :orderId', { orderId }) | ||
| .execute(); | ||
|
|
@@ -159,4 +170,28 @@ export class OrdersService { | |
|
|
||
| return orders; | ||
| } | ||
|
|
||
| async updateTrackingCostInfo(orderId: number, dto: TrackingCostDto) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update with product clarification - fms will initially have to enter both tracking link & cost but can edit either fields later on - could you reflect that in service logic here so that it requires both fields if order does not yet have tracking info |
||
| validateId(orderId, 'Order'); | ||
| if (!dto.trackingLink && !dto.shippingCost) { | ||
| throw new BadRequestException( | ||
| 'At least one of tracking link or shipping cost must be provided', | ||
| ); | ||
| } | ||
|
|
||
amywng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const order = await this.repo.findOneBy({ orderId }); | ||
| if (!order) { | ||
| throw new NotFoundException(`Order ${orderId} not found`); | ||
| } | ||
| if (order.status !== OrderStatus.SHIPPED) { | ||
| throw new BadRequestException( | ||
| 'Can only update tracking info for shipped orders', | ||
| ); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and can you add logic to support this: if order is pending and the tracking list & cost are provided then set the order status to shipped. |
||
| } | ||
|
|
||
| if (dto.trackingLink) order.trackingLink = dto.trackingLink; | ||
| if (dto.shippingCost) order.shippingCost = dto.shippingCost; | ||
|
|
||
| await this.repo.save(order); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.