11import Model , { attr , hasMany , belongsTo } from '@ember-data/model' ;
2- import { tracked } from '@glimmer/tracking' ;
32import { computed , action } from '@ember/object' ;
43import { getOwner } from '@ember/application' ;
54import { format as formatDate , formatDistanceToNow } from 'date-fns' ;
@@ -20,9 +19,6 @@ export default class ServiceRate extends Model {
2019 @belongsTo ( 'zone' ) zone ;
2120 @hasMany ( 'custom-field-value' , { async : false } ) custom_field_values ;
2221
23- /** @tracked */
24- @tracked perDropFees = [ ] ;
25-
2622 /** @attributes */
2723 @attr ( 'string' ) service_area_name ;
2824 @attr ( 'string' ) zone_name ;
@@ -120,46 +116,15 @@ export default class ServiceRate extends Model {
120116 return this . cod_calculation_method === 'percentage' ;
121117 }
122118
123- @computed ( 'max_distance' , 'max_distance_unit' , 'currency' , 'rate_fees' ) get rateFees ( ) {
124- const store = getOwner ( this ) . lookup ( 'service:store' ) ;
125- const unit = this . max_distance_unit ;
126- const currency = this . currency ;
119+ @computed ( 'rate_fees.@each.distance' , 'max_distance' ) get rateFees ( ) {
127120 const n = Math . max ( 0 , Number ( this . max_distance ) || 0 ) ;
128- const existing = ( this . rate_fees ?. toArray ?. ( ) ?? [ ] ) . slice ( ) ;
129- const byDistance = new Map ( existing . map ( ( r ) => [ r . distance , r ] ) ) ;
130-
131- const rows = [ ] ;
132- for ( let d = 0 ; d < n ; d ++ ) {
133- let rec = byDistance . get ( d ) ;
134- if ( ! rec ) {
135- rec = store . createRecord ( 'service-rate-fee' , {
136- distance : d ,
137- distance_unit : unit ,
138- fee : 0 ,
139- currency,
140- } ) ;
141- this . rate_fees . addObject ( rec ) ;
142- } else {
143- rec . setProperties ( { distance_unit : unit , currency } ) ;
144- }
145- rows . push ( rec ) ;
146- }
121+ const existing = ( this . rate_fees ?. toArray ?. ( ) ?? [ ] ) . filter ( ( r ) => r . distance !== null && r . distance !== undefined && ! r . isDeleted ) ;
147122
148- // note: do NOT prune here in a getter; do it via an explicit action
149- return rows ;
123+ // Return existing fees sorted by distance, filtered by max_distance
124+ return existing . filter ( ( r ) => r . distance >= 0 && r . distance < n ) . sort ( ( a , b ) => a . distance - b . distance ) ;
150125 }
151126
152127 /** @methods */
153- @action syncServiceRateFees ( ) {
154- if ( ! this . isFixedRate ) return ;
155- this . set ( 'rate_fees' , this . rateFees ) ;
156- }
157-
158- @action syncPerDropFees ( ) {
159- if ( ! this . isPerDrop ) return ;
160- this . set ( 'rate_fees' , this . perDropFees ) ;
161- }
162-
163128 @action createDefaultPerDropFee ( attributes = { } ) {
164129 const store = getOwner ( this ) . lookup ( 'service:store' ) ;
165130 return store . createRecord ( 'service-rate-fee' , {
@@ -173,28 +138,41 @@ export default class ServiceRate extends Model {
173138 }
174139
175140 @action addPerDropRateFee ( ) {
176- const last = this . perDropFees [ this . perDropFees . length - 1 ] ;
141+ const store = getOwner ( this ) . lookup ( 'service:store' ) ;
142+ const existingFees = this . rate_fees ?. toArray ?. ( ) ?? [ ] ;
143+ const last = existingFees [ existingFees . length - 1 ] ;
177144 const min = last ? last . max + 1 : 1 ;
178145 const max = min + 5 ;
179146
180- this . perDropFees = [
181- ...this . perDropFees ,
182- {
183- min : min ,
184- max : max ,
185- unit : 'waypoint' ,
186- fee : 0 ,
187- currency : this . currency ,
188- } ,
189- ] ;
147+ const newFee = store . createRecord ( 'service-rate-fee' , {
148+ min : min ,
149+ max : max ,
150+ unit : 'waypoint' ,
151+ fee : 0 ,
152+ currency : this . currency ,
153+ } ) ;
154+
155+ this . rate_fees . addObject ( newFee ) ;
190156 }
191157
192- @action removePerDropFee ( index ) {
193- if ( index === 0 ) return ;
194- this . perDropFees = [ ...this . perDropFees . filter ( ( _ , i ) => i !== index ) ] ;
158+ @action removePerDropFee ( fee ) {
159+ if ( ! fee || ! fee . destroyRecord ) return ;
160+ this . rate_fees . removeObject ( fee ) ;
161+ fee . destroyRecord ( ) ;
195162 }
196163
197164 @action resetPerDropFees ( ) {
198- this . perDropFees = [ this . createDefaultPerDropFee ( ) ] ;
165+ // Remove all existing per-drop fees
166+ const existingFees = this . rate_fees ?. toArray ?. ( ) ?? [ ] ;
167+ existingFees . forEach ( ( fee ) => {
168+ if ( fee . unit === 'waypoint' ) {
169+ this . rate_fees . removeObject ( fee ) ;
170+ fee . destroyRecord ( ) ;
171+ }
172+ } ) ;
173+
174+ // Add a new default per-drop fee
175+ const defaultFee = this . createDefaultPerDropFee ( ) ;
176+ this . rate_fees . addObject ( defaultFee ) ;
199177 }
200178}
0 commit comments