Skip to content

Commit

Permalink
FES-64-handle-unavailable-delivery-time-for-cart (#57)
Browse files Browse the repository at this point in the history
* planning date config, handle the empty timeslots
Co-authored-by: NHT <[email protected]>
  • Loading branch information
nfesta2023 authored Feb 18, 2024
1 parent e06bbf7 commit 65c77ad
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export default () => ({
},
featureFlag: process.env.FEATURE_FLAG || '',
ahamoveToken: process.env.AHAMOVE_TOKEN,
planningDay: 7, // the restaurant will plan new cooking schedule every Saturday (last until the end of the day)
});
17 changes: 14 additions & 3 deletions src/feature/cart/cart.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,20 @@ export class CartController {
lat,
utc_offset,
);
res.statusCode = 200;
res.message = 'Get available delivery time successfully';
res.data = timeSlots;
if (timeSlots.length > 0) {
res.statusCode = 200;
res.message = 'Get available delivery time successfully';
res.data = timeSlots;
} else if (timeSlots.length <= 0) {
const menuItems =
await this.commonService.getMenuItemByIds(menu_item_ids);
res.statusCode = 404;
res.message = 'No available delivery time';
res.data = await this.commonService.getPlanningDate(
menuItems[0].restaurant_id,
now,
);
}
} catch (error) {
if (error instanceof HttpException) {
res.statusCode = error.getStatus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { GeneralResponse } from 'src/dto/general-response.dto';
import { TimeSlot } from 'src/type';

export class GetAvailableDeliveryTimeResponse extends GeneralResponse {
data: TimeSlot[];
data: TimeSlot[] | number;
}
27 changes: 27 additions & 0 deletions src/feature/common/common.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ import { OperationHours } from 'src/entity/operation-hours.entity';
import { RestaurantDayOff } from 'src/entity/restaurant-day-off.entity';
import { ManualOpenRestaurant } from 'src/entity/manual-open-restaurant.entity';
import { AhamoveService } from 'src/dependency/ahamove/ahamove.service';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class CommonService {
constructor(
@Inject('FLAGSMITH_SERVICE') private readonly flagService: FlagsmithService,
@InjectEntityManager() private entityManager: EntityManager,
private readonly ahamoveService: AhamoveService,
private readonly configService: ConfigService,
) {}

async getRestaurantExtension(id: number): Promise<RestaurantExt[]> {
Expand Down Expand Up @@ -804,4 +806,29 @@ export class CommonService {
.getOne();
return sku;
} // end of getStandardSkuByMenuItem

async getPlanningDate(
restaurant_id: number,
now_timestamp: number,
): Promise<number> {
const restaurantUtcTimeZone = await this.getUtcTimeZone(restaurant_id);
const timeZoneOffset = restaurantUtcTimeZone * 60 * 60 * 1000; // Offset in milliseconds for EST
const adjustedNowTimestamp = now_timestamp + timeZoneOffset;
const adjustedNow = new Date(adjustedNowTimestamp);

//Get the planning day from the config
const planningDay = this.configService.get<number>('planningDay');
// Calculate the number of days to add to get to Planning Date
const daysToPlanningDay = planningDay - (adjustedNow.getUTCDay() + 1);

// Set the planning date
const planningDate = new Date(
adjustedNowTimestamp + daysToPlanningDay * 24 * 60 * 60 * 1000,
);
// Set the time to 23:59:59:999 (UTC timezone)
const planningDateTmestamp = planningDate.setUTCHours(23, 59, 59, 999);

//return data after adjusting with time offset
return planningDateTmestamp - timeZoneOffset;
} // end of getPlanningDate
}

0 comments on commit 65c77ad

Please sign in to comment.