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

Commit

Permalink
feat(#12): keep track of appointments requests and prefill them
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Sep 23, 2018
1 parent 73cd37e commit c2c4085
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class ItemAppointmentsDatePickerComponent extends AbstractPickAppointment
@Input() advertiserDates: number[];
@Input() unavailableAppointmentDates: number[];
@Input() rejectedAppointmentDates: number[]; // In case of to_reschedule appointments
@Input() prefillAppointmentsStartTimes: number[];

selectedAppointmentsStartTime: number[] = new Array();

Expand Down Expand Up @@ -69,15 +70,34 @@ export class ItemAppointmentsDatePickerComponent extends AbstractPickAppointment
const promises = new Array();
promises.push(this.hasManyPossibleTimeSlots());
promises.push(this.hasManyPossibleDays());
promises.push(this.prefillSelectedAppointments());

forkJoin(promises).subscribe(
(data: boolean[]) => {
this.manyPossibleTimeSlots = data[0];
this.manyPossibleDays = data[1];
([possibleTimeSlots, possibleDays, nothing]: [boolean, boolean, void]) => {
this.manyPossibleTimeSlots = possibleTimeSlots;
this.manyPossibleDays = possibleDays;
}
);
}

private prefillSelectedAppointments(): Promise<void> {
return new Promise<void>((resolve) => {
if (Comparator.hasElements(this.prefillAppointmentsStartTimes)) {
const prefillPickAppointmentTime: PickAppointmentTime[] = this.pickAppointmentTime.filter((appointmentTime: PickAppointmentTime) => {
return this.prefillAppointmentsStartTimes.indexOf(appointmentTime.startTime.getTime()) > -1;
});

if (Comparator.hasElements(prefillPickAppointmentTime)) {
prefillPickAppointmentTime.forEach((appointmentTime: PickAppointmentTime) => {
this.selectUnselectAppointment(appointmentTime);
});
}
}

resolve();
});
}

private hasManyPossibleTimeSlots(): Promise<{}> {
return new Promise((resolve) => {
if (!Comparator.hasElements(this.pickAppointmentTime)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<app-item-appointments-date-picker *ngIf="hasItemAppointment()" [advertiserDates]="initScheduledDates?.advertiserDates"
[unavailableAppointmentDates]="initScheduledDates?.unavailableAppointmentDates"
[rejectedAppointmentDates]="initScheduledDates?.rejectedAppointmentDates"
[prefillAppointmentsStartTimes]="initScheduledDates?.previousSelectedAppointmentsStartTimes"
(notifiySelected)="schedule($event)"></app-item-appointments-date-picker>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {NotificationService} from '../../../services/core/notification/notificat
import {ItemUsersService} from '../../../services/browse/item-users-service';
import {LikeService} from '../../../services/browse/like-service';
import {SubscriptionService} from '../../../services/core/user/subscription-service';
import {StorageService} from '../../../services/core/localstorage/storage-service';

@Component({
templateUrl: 'pick-item-appointments.html',
Expand Down Expand Up @@ -57,7 +58,8 @@ export class PickItemAppointmentsComponent extends AbstractItemsPage {
private likeService: LikeService,
private appointmentService: AppointmentService,
private userSessionService: UserSessionService,
private subscriptionService: SubscriptionService) {
private subscriptionService: SubscriptionService,
private storageService: StorageService) {

super(popoverController, itemUsersService);

Expand All @@ -78,7 +80,7 @@ export class PickItemAppointmentsComponent extends AbstractItemsPage {

private doProductCallback = () => {
this.initAndDoSchedule();
};
}

private async initAndDoSchedule() {
const loading: HTMLIonLoadingElement = await this.loadingController.create({});
Expand All @@ -98,7 +100,13 @@ export class PickItemAppointmentsComponent extends AbstractItemsPage {
return new Promise((resolve, reject) => {
const promise = this.hasExistingApplicant() ? this.updateWithNewSchedule() : this.createNewSchedule();

promise.then(() => {
promise.then(async () => {
try {
await this.storageService.savePrefillItemAppointmentsStartTimes(this.selectedAppointmentStartTimes);
} catch (err) {
// We could ignore this error, better if it works but what really matters have been done
}

resolve();
}, (errorResponse: HttpErrorResponse) => {
reject(errorResponse);
Expand All @@ -107,8 +115,8 @@ export class PickItemAppointmentsComponent extends AbstractItemsPage {
}

private updateWithNewSchedule(): Promise<{}> {
return new Promise((resolve, reject) => {
const applicant: Applicant = this.buildAppointmentApplicant();
return new Promise(async (resolve, reject) => {
const applicant: Applicant = await this.buildAppointmentApplicant();

this.appointmentService.updateApplicant(applicant).then((data: Applicant) => {
this.notificationAndInterests(applicant, false).then(() => {
Expand All @@ -121,8 +129,8 @@ export class PickItemAppointmentsComponent extends AbstractItemsPage {
}

private createNewSchedule(): Promise<{}> {
return new Promise((resolve, reject) => {
const applicant: Applicant = this.buildAppointmentApplicant();
return new Promise(async (resolve, reject) => {
const applicant: Applicant = await this.buildAppointmentApplicant();

this.appointmentService.createApplicant(applicant).then((data: Applicant) => {
// Appointment added, like item
Expand Down Expand Up @@ -185,21 +193,23 @@ export class PickItemAppointmentsComponent extends AbstractItemsPage {
});
}

private buildAppointmentApplicant(): Applicant {
const applicant: Applicant = this.hasExistingApplicant() ? this.existingApplicant :
new Applicant(this.item.appointment, this.user, this.item);
private buildAppointmentApplicant(): Promise<Applicant> {
return new Promise<Applicant>((resolve) => {
const applicant: Applicant = this.hasExistingApplicant() ? this.existingApplicant :
new Applicant(this.item.appointment, this.user, this.item);

applicant.status = this.RESOURCES.APPLICANT.STATUS.NEW;
applicant.status = this.RESOURCES.APPLICANT.STATUS.NEW;

for (let i: number = 0; i < this.selectedAppointmentStartTimes.length; i++) {
const agenda: ApplicantAgenda = new ApplicantAgenda();
agenda.when = new Date(this.selectedAppointmentStartTimes[i]);
agenda.status = this.RESOURCES.APPLICANT.AGENDA.STATUS.NEW;
for (let i: number = 0; i < this.selectedAppointmentStartTimes.length; i++) {
const agenda: ApplicantAgenda = new ApplicantAgenda();
agenda.when = new Date(this.selectedAppointmentStartTimes[i]);
agenda.status = this.RESOURCES.APPLICANT.AGENDA.STATUS.NEW;

applicant.agenda.push(agenda);
}
applicant.agenda.push(agenda);
}

return applicant;
resolve(applicant);
});
}

private hasExistingApplicant(): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ export abstract class AbstractPickAppointments {
Math.floor(this.pickAppointmentDate.length / this.RESOURCES.APPOINTMENT.DISPLAY.ALL_WEEK) *
this.RESOURCES.APPOINTMENT.DISPLAY.ALL_WEEK,
startTime: startTimeElement,
selected: (!this.onlySelectedDates && this.selectedDates != null &&
this.selectedDates.indexOf(startTimeElement.getTime()) > -1),
selected: this.isAppointmentTimeSelected(startTimeElement),
highlighted: this.highlightSpecialTime != null && startTimeElement.getTime() === this.highlightSpecialTime
};

Expand All @@ -192,6 +191,11 @@ export abstract class AbstractPickAppointments {
});
}

private isAppointmentTimeSelected(startTimeElement: Date): boolean {
return (!this.onlySelectedDates && this.selectedDates != null &&
this.selectedDates.indexOf(startTimeElement.getTime()) > -1);
}

private formateDisplayDate(toFormat: Date): string {
return '' + moment(toFormat).format('Do') + ' ' + moment(toFormat).format('MMM');
}
Expand Down Expand Up @@ -324,8 +328,8 @@ export abstract class AbstractPickAppointments {
const scrollRatio: number = (this.RESOURCES.APPOINTMENT.AGENDA.SCHEDULE.TIME_FRAME.MORNING.COUNT +
this.RESOURCES.APPOINTMENT.AGENDA.SCHEDULE.TIME_FRAME.AFTERNOON.COUNT) /
(this.RESOURCES.APPOINTMENT.AGENDA.SCHEDULE.TIME_FRAME.MORNING.COUNT +
this.RESOURCES.APPOINTMENT.AGENDA.SCHEDULE.TIME_FRAME.AFTERNOON.COUNT +
this.RESOURCES.APPOINTMENT.AGENDA.SCHEDULE.TIME_FRAME.EVENING.COUNT);
this.RESOURCES.APPOINTMENT.AGENDA.SCHEDULE.TIME_FRAME.AFTERNOON.COUNT +
this.RESOURCES.APPOINTMENT.AGENDA.SCHEDULE.TIME_FRAME.EVENING.COUNT);

scrollElement.scrollTop = scrollElement.scrollHeight * scrollRatio;
}
Expand Down
11 changes: 9 additions & 2 deletions src/app/services/core/appointment/item-appointment-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ import {Comparator, Converter} from '../utils/utils';

// Services
import {AppointmentService} from './appointment-service';
import {StorageService} from '../localstorage/storage-service';

export interface InitScheduledDates {
advertiserDates: number[];
unavailableAppointmentDates: number[];
rejectedAppointmentDates: number[];
previousSelectedAppointmentsStartTimes: number[];
}

@Injectable({
providedIn: 'root'
})
export class ItemAppointmentService {

constructor(private appointmentService: AppointmentService) {
constructor(private appointmentService: AppointmentService,
private storageService: StorageService) {

}

Expand All @@ -35,6 +38,7 @@ export class ItemAppointmentService {
item.appointment._id, item.appointment.attendance));
promises.push(this.buildUnavailableAppointments(existingApplicant));
promises.push(this.appointmentService.getMyApplicants(null));
promises.push(this.storageService.retrievePrefillItemAppointmentsStartTimes());

forkJoin(promises).subscribe(
(data: number[][]) => {
Expand All @@ -46,11 +50,14 @@ export class ItemAppointmentService {
let allUnavailableDates: number[] = allAlreadyScheduledDates;
allUnavailableDates = allUnavailableDates.concat(data[2]);

const previousStartTimes: number[] = data[4];

this.hasStillAvailableDates(data[0], allUnavailableDates).then((hasStillAvailableDates: boolean) => {
resolve({
advertiserDates: hasStillAvailableDates ? data[0] : new Array(),
unavailableAppointmentDates: allAlreadyScheduledDates,
rejectedAppointmentDates: data[2]
rejectedAppointmentDates: data[2],
previousSelectedAppointmentsStartTimes: previousStartTimes ? previousStartTimes : new Array()
});
});
}
Expand Down
8 changes: 8 additions & 0 deletions src/app/services/core/localstorage/storage-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,12 @@ export class StorageService {
return this.storage.get('fluster_login_state');
}

savePrefillItemAppointmentsStartTimes(selectedAppointmentsStartTimes: number[]): Promise<number[]> {
return this.storage.set('fluster_prefill_item_appointments', selectedAppointmentsStartTimes);
}

retrievePrefillItemAppointmentsStartTimes(): Promise<number[]> {
return this.storage.get('fluster_prefill_item_appointments');
}

}

0 comments on commit c2c4085

Please sign in to comment.