Skip to content
This repository has been archived by the owner on May 29, 2020. It is now read-only.

Commit

Permalink
Added merge possibilities for steps and delete case handle
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Capelli committed Feb 20, 2020
1 parent 1b3833b commit 6bcc03e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
4 changes: 0 additions & 4 deletions helpers/date-helpers.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import Luxon from 'luxon';

const { DateTime } = Luxon;

export const sortByDate = (array) => array.sort((a, b) => {
const startA = a.date;
const startB = b.date;
Expand Down
9 changes: 5 additions & 4 deletions helpers/step-management/generator.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import {
CREATED,
DELIVERED,
STARTED, VALIDATED,
} from '../../models/status';
import { DEPARTURE, ARRIVAL } from './types';
import Step from './schema';
import { isToday } from '../date-helpers';

export default (ride) => {
const steps = [];
if (ride.status !== DELIVERED) {
steps.push(new Step(ARRIVAL, ride));

const shouldGenerateDualStep = ride.status === VALIDATED || ride.status === STARTED;
if (ride.status !== DELIVERED && isToday(ride.start)) {
const shouldGenerateDualStep = ride.status === CREATED || ride.status === VALIDATED || ride.status === STARTED;
if (shouldGenerateDualStep) {
steps.push(new Step(DEPARTURE, ride));
}
steps.push(new Step(ARRIVAL, ride));
}
return steps;
};
2 changes: 1 addition & 1 deletion helpers/step-management/merge.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default (steps) => steps.reduce((acc, step) => {
],
phone: [
...previous.phone,
step.phone,
...step.phone,
],
passengersCount: {
key: previous.passengersCount.key,
Expand Down
2 changes: 1 addition & 1 deletion helpers/step-management/schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class Step {
this.date = type === ARRIVAL ? ride.end : ride.start;
this.destination = type === ARRIVAL ? ride.arrival.label : ride.departure.label;
this.status = type === ARRIVAL ? WAITING : VALIDATED;
this.phone = [ride.phone];
this.phone = ride.phone ? [ride.phone] : [];
this.generatePassengerCount(ride.passengersCount);
this.generateDetails(
ride.comments,
Expand Down
7 changes: 7 additions & 0 deletions models/status.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ export const CANCEL_TECHNICAL = 'cancel_technical';
export const CANCEL_REQUESTED_CUSTOMER = 'cancel_requested_by_customer';
export const CANCEL_CUSTOMER_OVERLOAD = 'cancel_customer_overload';
export const CANCEL_CUSTOMER_MISSING = 'cancel_customer_missing';
export const CANCEL_STATUSES = [
CANCEL,
CANCEL_TECHNICAL,
CANCEL_REQUESTED_CUSTOMER,
CANCEL_CUSTOMER_OVERLOAD,
CANCEL_CUSTOMER_MISSING,
];

export const CANCELABLE = [CREATED, VALIDATED, ACCEPTED, STARTED, WAITING, IN_PROGRESS];

Expand Down
10 changes: 8 additions & 2 deletions routes/rides.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Luxon from 'luxon';
import {
CANCEL_REQUESTED_CUSTOMER,
CANCELED_STATUSES, CREATE, DELIVERED, DRAFTED,
CANCEL_STATUSES,
} from '../models/status';
import maskOutput, { cleanObject } from '../middlewares/mask-output';
import contentNegociation from '../middlewares/content-negociation';
Expand Down Expand Up @@ -140,7 +141,7 @@ const router = generateCRUD(Ride, {
if (ctx.query && ctx.query.step) {
const { step } = ctx.query;
step.status = ctx.body.status;
ioEmit(ctx, cleanObject(step), 'rideUpdate', driverRoom);
ioEmit(ctx, [step], 'rideUpdate', driverRoom);
}

ioEmit(ctx, cleanObject(ctx.body), 'rideUpdate', rooms);
Expand Down Expand Up @@ -305,10 +306,15 @@ router.post(

const { rooms, driverRoom } = getRooms(ride);

const isActionCancel = CANCEL_STATUSES.filter((status) => action === status).length > 0;
if (isActionCancel) {
ioEmit(ctx, ride._id, 'deleteStep', driverRoom);
}

if (ctx.query && ctx.query.step) {
const { step } = ctx.query;
step.status = ctx.body.status;
ioEmit(ctx, cleanObject(step), 'rideUpdate', driverRoom);
ioEmit(ctx, cleanObject([step]), 'rideUpdate', driverRoom);
}

ioEmit(ctx, cleanObject(ctx.body), 'rideUpdate', rooms);
Expand Down

0 comments on commit 6bcc03e

Please sign in to comment.