Skip to content

Commit

Permalink
fix: delete time span when delete time slot
Browse files Browse the repository at this point in the history
  • Loading branch information
ckhandla94 committed Aug 2, 2020
1 parent e312d80 commit ca7a46d
Show file tree
Hide file tree
Showing 18 changed files with 242 additions and 181 deletions.
4 changes: 4 additions & 0 deletions apps/api/src/app/core/moment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as momentDefault from 'moment';
import { extendMoment } from 'moment-range';

export const moment = extendMoment(momentDefault);
2 changes: 1 addition & 1 deletion apps/api/src/app/hubstaff/hubstaff.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import { UserService } from '../user/user.service';
import { EmployeeGetCommand } from '../employee/commands/employee.get.command';
import * as moment from 'moment';
import {
TimeLogCreateCommand,
TimeSlotCreateCommand,
TimesheetGetCommand,
TimesheetCreateCommand,
Expand All @@ -49,6 +48,7 @@ import {
import { environment } from '@env-api/environment';
import { getDummyImage } from '../core';
import { TenantService } from '../tenant/tenant.service';
import { TimeLogCreateCommand } from '../timesheet/time-log/commands/time-log-create.command';

@Injectable()
export class HubstaffService {
Expand Down
2 changes: 0 additions & 2 deletions apps/api/src/app/timesheet/commands/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { TimeLogCreateHandler } from './time-log-create.handler';
import { TimeSlotCreateHandler } from './time-slot-create.handler';
import { TimesheetCreateHandler } from './timesheet-create.handler';
import { TimesheetGetHandler } from './timesheet-get.handler';
Expand All @@ -7,7 +6,6 @@ import { ActivityCreateHandler } from './activity-create.handler';
import { TimeSlotMinuteCreateHandler } from './time-slot-minute-create.handler';

export const CommandHandlers = [
TimeLogCreateHandler,
TimeSlotCreateHandler,
TimeSlotMinuteCreateHandler,
TimesheetCreateHandler,
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion apps/api/src/app/timesheet/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './time-log-create.command';
export * from './time-slot-create.command';
export * from './timesheet-create.command';
export * from './timesheet-get.command';
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { Repository } from 'typeorm';
import { TimeSlotService } from '../../../time-slot/time-slot.service';
import * as _ from 'underscore';
import { DeleteTimeSpanCommand } from '../delete-time-span.command';
import * as moment from 'moment';
import { TimeLogUpdateCommand } from '../time-log-update.command';
import { TimeLogDeleteCommand } from '../time-log-delete.command';
import { moment } from '../../../../core/moment';

@CommandHandler(DeleteTimeSpanCommand)
export class DeleteTimeSpanHandler
Expand All @@ -24,12 +24,23 @@ export class DeleteTimeSpanHandler

const { start, end } = newTime;

console.log('deleteTimeSpan', {
startedAt: timeLog.startedAt,
stoppedAt: timeLog.stoppedAt,
start,
end
});
const newTimeRange = moment.range(start, end);
const dbTimeRange = moment.range(timeLog.startedAt, timeLog.stoppedAt);

/* Check is overlaping time or not.
*/
if (!newTimeRange.overlaps(dbTimeRange, { adjacent: false })) {
console.log(
'deleteTimeSpan not overlaping',
newTimeRange,
dbTimeRange
);
// await this.commandBus.execute(
// new TimeLogDeleteCommand(timeLog, false)
// );
return false;
}

if (
moment(timeLog.startedAt).isBetween(
moment(start),
Expand Down Expand Up @@ -116,14 +127,18 @@ export class DeleteTimeSpanHandler
reamingDueration
);
if (reamingDueration > 0) {
await this.timeLogRepository.update(
timeLog.timeSlots = await this.timeSlotService.getTimeSlots(
{
id: timeLog.id
},
{
stoppedAt: start
startDate: timeLog.startedAt,
endDate: moment(timeLog.stoppedAt)
.subtract(1, 'second')
.toDate()
}
);

timeLog.stoppedAt = start;

await this.timeLogRepository.save(timeLog);
} else {
/* Delete if reaming dueration 0 seconds */
await this.commandBus.execute(
Expand All @@ -132,31 +147,42 @@ export class DeleteTimeSpanHandler
}
} else {
/* Split database time in two entries.
* New Start time New Stop time
* New Start time (start) New Stop time (end)
* |----------------------------|
* DB Start Time DB Stop Time
* DB Start Time (startedAt) DB Stop Time (stoppedAt)
* |--------------------------------------------------|
*/
console.log(
'deleteTimeSpan Split database time in two entries'
);
const reamingDueration = moment(end).diff(
const reamingDueration = moment(start).diff(
moment(timeLog.startedAt),
'seconds'
);
console.log(
'deleteTimeSpan reamingDueration',
reamingDueration
);

const timeLogClone: TimeLog = _.omit(timeLog, [
'createdAt',
'updatedAt',
'id'
]);

if (reamingDueration > 0) {
await this.timeLogRepository.update(
{
id: timeLog.id
},
timeLog.stoppedAt = start;

timeLog.timeSlots = await this.timeSlotService.getTimeSlots(
{
stoppedAt: start
startDate: timeLog.startedAt,
endDate: moment(timeLog.stoppedAt)
.subtract(1, 'second')
.toDate()
}
);

await this.timeLogRepository.save(timeLog);
} else {
/* Delete if reaming dueration 0 seconds */
await this.commandBus.execute(
Expand All @@ -170,25 +196,32 @@ export class DeleteTimeSpanHandler
end
);

const newLog = _.omit(timeLog, [
'createdAt',
'updatedAt',
'id'
]);
const newLog = timeLogClone;
newLog.startedAt = end;
const newLogreamingDueration = moment(newLog.stoppedAt).diff(

// const range = moment.range(newLog.startedAt, newLog.stoppedAt)
// console.log(range, Array.from(range.by('minutes', { step: 10, excludeEnd: true })).map(m => m.format('YYYY-MM-DD HH:mm:ss')));

newLog.timeSlots = await this.timeSlotService.getTimeSlots({
startDate: newLog.startedAt,
endDate: moment(newLog.stoppedAt)
.subtract(1, 'second')
.toDate()
});

const newLogReamingDueration = moment(newLog.stoppedAt).diff(
moment(newLog.startedAt),
'seconds'
);

console.log(
'deleteTimeSpan newLogreamingDueration',
newLogreamingDueration,
'deleteTimeSpan newLogReamingDueration',
newLogReamingDueration,
newLog
);
/* Insert if reaming dueration is more 0 seconds */
if (newLogreamingDueration > 0) {
await this.timeLogRepository.insert(newLog);
if (newLogReamingDueration > 0) {
await this.timeLogRepository.save(newLog);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { Repository } from 'typeorm';
import { TimeSlotService } from '../../../time-slot/time-slot.service';
import { TimesheetFirstOrCreateCommand } from '../../../timesheet/commands/timesheet-first-or-create.command';
import { TimesheetRecalculateCommand } from '../../../timesheet/commands/timesheet-recalculate.command';
import * as moment from 'moment';
import * as _ from 'underscore';

@CommandHandler(TimeLogCreateCommand)
export class TimeLogCreateHandler
Expand All @@ -26,13 +28,13 @@ export class TimeLogCreateHandler
);

const newTimeLog = new TimeLog({
startedAt: input.startedAt,
stoppedAt: input.stoppedAt,
startedAt: moment.utc(input.startedAt).toDate(),
stoppedAt: moment.utc(input.stoppedAt).toDate(),
timesheetId: timesheet.id,
employeeId: input.employeeId,
projectId: input.projectId || null,
taskId: input.taskId || null,
clientId: input.clientId || null,
organizationContactId: input.organizationContactId || null,
logType: input.logType || TimeLogType.MANUAL,
description: input.description || '',
isBillable: input.isBillable || false
Expand All @@ -49,8 +51,42 @@ export class TimeLogCreateHandler
mouse: 0,
overall: 0
}));
await this.timeSlotService.bulkCreate(timeSlots);
newTimeLog.timeSlots = timeSlots;

if (input.timeSlots) {
/*
* Merge blank timeslot if missing in request.
* I.e
* Time Logs is : 04:00:00 to 05:00:00 and pass time slots for 04:00:00, 04:20:00, 04:30:00, 04:40:00
* then it will add 04:10:00, 04:50:00 as blank time slots in array to instert
*/
input.timeSlots = input.timeSlots.map((timeSlot) => {
timeSlot.startedAt = moment.utc(input.startedAt).toDate();
timeSlot.employeeId = input.employeeId;
return timeSlot;
});

timeSlots = timeSlots.map((blankTimeSlot) => {
let timeSlot = input.timeSlots.find((requestTimeSlot) => {
return (
moment
.utc(requestTimeSlot.startedAt)
.format('YYYY-MM-DD HH:mm') ===
moment
.utc(blankTimeSlot.startedAt)
.format('YYYY-MM-DD HH:mm')
);
});

timeSlot = timeSlot ? timeSlot : blankTimeSlot;
timeSlot.employeeId = input.employeeId;

console.log(timeSlot);
return timeSlot;
});
}

newTimeLog.timeSlots = await this.timeSlotService.bulkCreate(timeSlots);

await this.timeLogRepository.save(newTimeLog);

await this.commandBus.execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class TimeLogDeleteHandler
try {
await Promise.all(timesheetPromises);
} catch (error) {
console.log(error);
console.log('TimeLogDeleteHandler', { error });
}

return deleteResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export class TimeLogUpdateHandler
new TimesheetRecalculateCommand(timeLog.timesheetId)
);
}
timeLog.timeSlots = updateTimeSlots;
this.timeLogRepository.save(timeLog);

return timeLog;
}
Expand Down
3 changes: 1 addition & 2 deletions apps/api/src/app/timesheet/time-log/time-log.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ export class TimeLogController extends CrudController<TimeLog> {
@Param('id') id: string,
@Body() entity: IManualTimeInput
): Promise<TimeLog> {
entity.id = id;
return this.timeLogService.updateManualTime(entity);
return this.timeLogService.updateTime(id, entity);
}

@ApiOperation({ summary: 'Delete time log' })
Expand Down
Loading

0 comments on commit ca7a46d

Please sign in to comment.