55} from './cancellation-fee.service' ;
66import { Shipment } from './entities/shipment.entity' ;
77import { ShipmentStatus } from '../common/enums/shipment-status.enum' ;
8+ import { UserRole } from '../common/enums/role.enum' ;
89
910function makeShipment ( overrides : Partial < Shipment > = { } ) : Shipment {
1011 return {
@@ -116,7 +117,11 @@ describe('CancellationFeeService', () => {
116117 it ( 'stamps the fee and an audit note onto the shipment' , ( ) => {
117118 const shipment = makeShipment ( { status : ShipmentStatus . ACCEPTED } ) ;
118119
119- const fee = service . applyTo ( shipment , ShipmentStatus . ACCEPTED ) ;
120+ const fee = service . applyTo (
121+ shipment ,
122+ UserRole . SHIPPER ,
123+ ShipmentStatus . ACCEPTED ,
124+ ) ;
120125
121126 expect ( fee ) . toBe ( 20 ) ;
122127 expect ( shipment . cancellationFee ) . toBe ( 20 ) ;
@@ -126,7 +131,7 @@ describe('CancellationFeeService', () => {
126131 it ( 'defaults to the shipment’s current status' , ( ) => {
127132 const shipment = makeShipment ( { status : ShipmentStatus . IN_TRANSIT } ) ;
128133
129- expect ( service . applyTo ( shipment ) ) . toBe ( 50 ) ;
134+ expect ( service . applyTo ( shipment , UserRole . SHIPPER ) ) . toBe ( 50 ) ;
130135 expect ( shipment . cancellationFee ) . toBe ( 50 ) ;
131136 } ) ;
132137
@@ -136,7 +141,7 @@ describe('CancellationFeeService', () => {
136141 notes : 'Handle with care — fragile electronics' ,
137142 } ) ;
138143
139- service . applyTo ( shipment ) ;
144+ service . applyTo ( shipment , UserRole . SHIPPER ) ;
140145
141146 expect ( shipment . notes ) . toBe (
142147 'Handle with care — fragile electronics\nCancellation fee: 50 USD' ,
@@ -146,22 +151,44 @@ describe('CancellationFeeService', () => {
146151 it ( 'records a zero fee for a cancelled-before-accept shipment' , ( ) => {
147152 const shipment = makeShipment ( { status : ShipmentStatus . PENDING } ) ;
148153
149- expect ( service . applyTo ( shipment ) ) . toBe ( 0 ) ;
154+ expect ( service . applyTo ( shipment , UserRole . SHIPPER ) ) . toBe ( 0 ) ;
150155 expect ( shipment . cancellationFee ) . toBe ( 0 ) ;
151156 expect ( shipment . notes ) . toBe ( 'Cancellation fee: 0 USD' ) ;
152157 } ) ;
153158
154159 it ( 'refuses to apply a fee to a non-cancellable shipment' , ( ) => {
155160 const shipment = makeShipment ( { status : ShipmentStatus . DELIVERED } ) ;
156161
157- expect ( ( ) => service . applyTo ( shipment ) ) . toThrow ( BadRequestException ) ;
162+ expect ( ( ) => service . applyTo ( shipment , UserRole . SHIPPER ) ) . toThrow (
163+ BadRequestException ,
164+ ) ;
158165 expect ( shipment . cancellationFee ) . toBeNull ( ) ;
159166 } ) ;
160167
161168 it ( 'coerces a stringified numeric price (Postgres numeric) safely' , ( ) => {
162169 const shipment = makeShipment ( { price : '1000.00' as never } ) ;
163170
164- expect ( service . applyTo ( shipment , ShipmentStatus . IN_TRANSIT ) ) . toBe ( 250 ) ;
171+ expect (
172+ service . applyTo (
173+ shipment ,
174+ UserRole . SHIPPER ,
175+ ShipmentStatus . IN_TRANSIT ,
176+ ) ,
177+ ) . toBe ( 250 ) ;
178+ } ) ;
179+
180+ it ( 'waives the fee when a carrier cancels' , ( ) => {
181+ const shipment = makeShipment ( { status : ShipmentStatus . IN_TRANSIT } ) ;
182+
183+ expect ( service . applyTo ( shipment , UserRole . CARRIER ) ) . toBe ( 0 ) ;
184+ expect ( shipment . cancellationFee ) . toBe ( 0 ) ;
185+ expect ( shipment . notes ) . toBe ( 'Cancellation fee: 0 USD' ) ;
186+ } ) ;
187+
188+ it ( 'uses the status tier when an admin cancels on the shipper side' , ( ) => {
189+ const shipment = makeShipment ( { status : ShipmentStatus . ACCEPTED } ) ;
190+
191+ expect ( service . applyTo ( shipment , UserRole . ADMIN ) ) . toBe ( 20 ) ;
165192 } ) ;
166193 } ) ;
167194
0 commit comments