/// <summary> /// Adds the shift to Kronos and the database. /// </summary> /// <param name="shift">The shift to add.</param> /// <param name="user">The user the shift is for.</param> /// <param name="mappedTeam">The team the user is in.</param> /// <returns>A response for teams.</returns> public async Task <ShiftsIntegResponse> CreateShiftInKronosAsync(ShiftsShift shift, AllUserMappingEntity user, TeamToDepartmentJobMappingEntity mappedTeam) { // The connector does not support drafting entities as it is not possible to draft shifts in Kronos. // Likewise there is no share schedule WFI call. if (shift.DraftShift != null) { return(ResponseHelper.CreateBadResponse(shift.Id, error: "Creating a shift as a draft is not supported for your team in Teams. Please publish changes directly using the 'Share' button.")); } if (shift.SharedShift == null) { return(ResponseHelper.CreateBadResponse(shift.Id, error: "An unexpected error occured. Could not create the shift.")); } if (shift.SharedShift.Activities.Any()) { return(ResponseHelper.CreateBadResponse(shift.Id, error: "Adding activities to shifts is not supported for your team in Teams. Remove all activities and try sharing again.")); } if (user.ErrorIfNull(shift.Id, "User could not be found.", out var response)) { return(response); } var allRequiredConfigurations = await this.utility.GetAllConfigurationsAsync().ConfigureAwait(false); if ((allRequiredConfigurations?.IsAllSetUpExists).ErrorIfNull(shift.Id, "App configuration incorrect.", out response)) { return(response); } var kronosStartDateTime = this.utility.UTCToKronosTimeZone(shift.SharedShift.StartDateTime, mappedTeam.KronosTimeZone); var kronosEndDateTime = this.utility.UTCToKronosTimeZone(shift.SharedShift.EndDateTime, mappedTeam.KronosTimeZone); var commentTimeStamp = this.utility.UTCToKronosTimeZone(DateTime.UtcNow, mappedTeam.KronosTimeZone).ToString(CultureInfo.InvariantCulture); var comments = XmlHelper.GenerateKronosComments(shift.SharedShift.Notes, this.appSettings.ShiftNotesCommentText, commentTimeStamp); var creationResponse = await this.shiftsActivity.CreateShift( new Uri(allRequiredConfigurations.WfmEndPoint), allRequiredConfigurations.KronosSession, this.utility.FormatDateForKronos(kronosStartDateTime), this.utility.FormatDateForKronos(kronosEndDateTime), kronosEndDateTime.Day > kronosStartDateTime.Day, Utility.OrgJobPathKronosConversion(user.PartitionKey), user.RowKey, kronosStartDateTime.TimeOfDay.ToString(), kronosEndDateTime.TimeOfDay.ToString(), comments).ConfigureAwait(false); if (creationResponse.Status != Success) { return(ResponseHelper.CreateBadResponse(shift.Id, error: "Shift was not created successfully in Kronos.")); } var monthPartitionKey = Utility.GetMonthPartition(this.utility.FormatDateForKronos(kronosStartDateTime), this.utility.FormatDateForKronos(kronosEndDateTime)); await this.CreateAndStoreShiftMapping(shift, user, mappedTeam, monthPartitionKey).ConfigureAwait(false); return(ResponseHelper.CreateSuccessfulResponse(shift.Id)); }
/// <summary> /// Deletes the shift from Kronos and the database. /// </summary> /// <param name="shift">The shift to remove.</param> /// <param name="user">The user the shift is for.</param> /// <param name="mappedTeam">The team the user is in.</param> /// <returns>A response for teams.</returns> public async Task <ShiftsIntegResponse> DeleteShiftInKronosAsync(ShiftsShift shift, AllUserMappingEntity user, TeamToDepartmentJobMappingEntity mappedTeam) { if (shift.SharedShift == null) { return(ResponseHelper.CreateBadResponse(shift.Id, error: "An unexpected error occured. Could not delete the shift.")); } if (user.ErrorIfNull(shift.Id, "User could not be found.", out var response)) { return(response); } var allRequiredConfigurations = await this.utility.GetAllConfigurationsAsync().ConfigureAwait(false); if ((allRequiredConfigurations?.IsAllSetUpExists == false).ErrorIfNull(shift.Id, "App configuration incorrect.", out response)) { return(response); } // Convert to Kronos local time. var kronosStartDateTime = this.utility.UTCToKronosTimeZone(shift.SharedShift.StartDateTime, mappedTeam.KronosTimeZone); var kronosEndDateTime = this.utility.UTCToKronosTimeZone(shift.SharedShift.EndDateTime, mappedTeam.KronosTimeZone); var deletionResponse = await this.shiftsActivity.DeleteShift( new Uri(allRequiredConfigurations.WfmEndPoint), allRequiredConfigurations.KronosSession, this.utility.FormatDateForKronos(kronosStartDateTime), this.utility.FormatDateForKronos(kronosEndDateTime), kronosEndDateTime.Day > kronosStartDateTime.Day, Utility.OrgJobPathKronosConversion(user.PartitionKey), user.RowKey, kronosStartDateTime.TimeOfDay.ToString(), kronosEndDateTime.TimeOfDay.ToString()).ConfigureAwait(false); if (deletionResponse.Status != Success) { return(ResponseHelper.CreateBadResponse(shift.Id, error: "Shift was not successfully removed from Kronos.")); } await this.DeleteShiftMapping(shift).ConfigureAwait(false); #pragma warning disable CS4014 // We do not want to await this call as we need the shift to be deleted in Teams before sharing the schedule. Task.Run(() => this.ShareScheduleAfterShiftDeletion(shift, mappedTeam, allRequiredConfigurations)); #pragma warning restore CS4014 return(ResponseHelper.CreateSuccessfulResponse(shift.Id)); }
/// <summary> /// Edits a shift in Kronos and updates the database. /// </summary> /// <param name="editedShift">The shift to edit.</param> /// <param name="user">The user the shift is for.</param> /// <param name="mappedTeam">The team the user is in.</param> /// <returns>A response for teams.</returns> public async Task <ShiftsIntegResponse> EditShiftInKronosAsync(ShiftsShift editedShift, AllUserMappingEntity user, TeamToDepartmentJobMappingEntity mappedTeam) { // The connector does not support drafting entities as it is not possible to draft shifts in Kronos. // Likewise there is no share schedule WFI call. if (editedShift.DraftShift != null) { return(ResponseHelper.CreateBadResponse(editedShift.Id, error: "Editing a shift as a draft is not supported for your team in Teams. Please publish changes directly using the 'Share' button.")); } if (editedShift.SharedShift == null) { return(ResponseHelper.CreateBadResponse(editedShift.Id, error: "An unexpected error occured. Could not edit the shift.")); } // We use the display name to indicate shift transfers. As we cannot support editing shifts // with a transfer we block edits on shifts containing the transfer string. if (editedShift.SharedShift.DisplayName.Contains(appSettings.TransferredShiftDisplayName)) { return(ResponseHelper.CreateBadResponse(editedShift.Id, error: "You can't edit a shift that includes a shift transfer. Please make your changes in Kronos")); } // We do not support editing activities in Teamsand cannot support editing shift transfers // therefore we only expect activities with the regular segment type. Anything else means the // manager has modified or added an activity. var invalidActivities = editedShift.SharedShift.Activities.Where(x => x.DisplayName != ApiConstants.RegularSegmentType); if (invalidActivities.Any()) { return(ResponseHelper.CreateBadResponse(editedShift.Id, error: "Editing shift activities is not supported for your team in Teams.")); } var allRequiredConfigurations = await this.utility.GetAllConfigurationsAsync().ConfigureAwait(false); if ((allRequiredConfigurations?.IsAllSetUpExists).ErrorIfNull(editedShift.Id, "App configuration incorrect.", out var response)) { return(response); } // We need to get all other shifts the employee works that day. var kronosStartDateTime = this.utility.UTCToKronosTimeZone(editedShift.SharedShift.StartDateTime, mappedTeam.KronosTimeZone); var kronosEndDateTime = this.utility.UTCToKronosTimeZone(editedShift.SharedShift.EndDateTime, mappedTeam.KronosTimeZone); var monthPartitionKey = Utility.GetMonthPartition(this.utility.FormatDateForKronos(kronosStartDateTime), this.utility.FormatDateForKronos(kronosEndDateTime)); var shiftToReplace = await this.shiftMappingEntityProvider.GetShiftMappingEntityByRowKeyAsync(editedShift.Id).ConfigureAwait(false); var shiftToReplaceStartDateTime = this.utility.UTCToKronosTimeZone(shiftToReplace.ShiftStartDate, mappedTeam.KronosTimeZone); var shiftToReplaceEndDateTime = this.utility.UTCToKronosTimeZone(shiftToReplace.ShiftEndDate, mappedTeam.KronosTimeZone); var commentTimeStamp = this.utility.UTCToKronosTimeZone(DateTime.UtcNow, mappedTeam.KronosTimeZone).ToString(CultureInfo.InvariantCulture); var shiftComments = XmlHelper.GenerateEditedShiftKronosComments(editedShift.SharedShift.Notes, this.appSettings.ShiftNotesCommentText, commentTimeStamp); var editResponse = await this.shiftsActivity.EditShift( new Uri(allRequiredConfigurations.WfmEndPoint), allRequiredConfigurations.KronosSession, this.utility.FormatDateForKronos(kronosStartDateTime), this.utility.FormatDateForKronos(kronosEndDateTime), kronosEndDateTime.Day > kronosStartDateTime.Day, Utility.OrgJobPathKronosConversion(user.PartitionKey), user.RowKey, kronosStartDateTime.TimeOfDay.ToString(), kronosEndDateTime.TimeOfDay.ToString(), this.utility.FormatDateForKronos(shiftToReplaceStartDateTime), this.utility.FormatDateForKronos(shiftToReplaceEndDateTime), shiftToReplaceStartDateTime.TimeOfDay.ToString(), shiftToReplaceEndDateTime.TimeOfDay.ToString(), shiftComments).ConfigureAwait(false); if (editResponse.Status != Success) { return(ResponseHelper.CreateBadResponse(editedShift.Id, error: "Shift could not be edited in Kronos.")); } await this.DeleteShiftMapping(editedShift).ConfigureAwait(false); await this.CreateAndStoreShiftMapping(editedShift, user, mappedTeam, monthPartitionKey).ConfigureAwait(false); return(ResponseHelper.CreateSuccessfulResponse(editedShift.Id)); }
/// <summary> /// Creates an open shift in Kronos. /// </summary> /// <param name="openShift">The open shift entity to create in Kronos.</param> /// <param name="team">The team the open shift belongs to.</param> /// <returns>A response to return to teams.</returns> public async Task <ShiftsIntegResponse> CreateOpenShiftInKronosAsync(Models.IntegrationAPI.OpenShiftIS openShift, TeamToDepartmentJobMappingEntity team) { // The connector does not support drafting entities as it is not possible to draft shifts in Kronos. // Likewise there is no share schedule WFI call. if (openShift.DraftOpenShift != null) { return(ResponseHelper.CreateBadResponse(openShift.Id, error: "Creating an open shift as a draft is not supported for your team in Teams. Please publish changes directly using the 'Share' button.")); } if (openShift.SharedOpenShift == null) { return(ResponseHelper.CreateBadResponse(openShift.Id, error: "An unexpected error occured. Could not create open shift.")); } if (openShift.SharedOpenShift.Activities.Any()) { return(ResponseHelper.CreateBadResponse(openShift.Id, error: "Adding activities to open shifts is not supported for your team in Teams. Remove all activities and try sharing again.")); } var allRequiredConfigurations = await this.utility.GetAllConfigurationsAsync().ConfigureAwait(false); if ((allRequiredConfigurations?.IsAllSetUpExists).ErrorIfNull(openShift.Id, "App configuration incorrect.", out var response)) { return(response); } var possibleTeams = await this.teamDepartmentMappingProvider.GetMappedTeamDetailsBySchedulingGroupAsync(team.TeamId, openShift.SchedulingGroupId).ConfigureAwait(false); var openShiftOrgJobPath = possibleTeams.FirstOrDefault().RowKey; var commentTimeStamp = this.utility.UTCToKronosTimeZone(DateTime.UtcNow, team.KronosTimeZone).ToString(CultureInfo.InvariantCulture); var comments = XmlHelper.GenerateKronosComments(openShift.SharedOpenShift.Notes, this.appSettings.ShiftNotesCommentText, commentTimeStamp); var openShiftDetails = new { KronosStartDateTime = this.utility.UTCToKronosTimeZone(openShift.SharedOpenShift.StartDateTime, team.KronosTimeZone), KronosEndDateTime = this.utility.UTCToKronosTimeZone(openShift.SharedOpenShift.EndDateTime, team.KronosTimeZone), DisplayName = openShift.SharedOpenShift.DisplayName, }; var creationResponse = await this.openShiftActivity.CreateOpenShiftAsync( new Uri(allRequiredConfigurations.WfmEndPoint), allRequiredConfigurations.KronosSession, this.utility.FormatDateForKronos(openShiftDetails.KronosStartDateTime), this.utility.FormatDateForKronos(openShiftDetails.KronosEndDateTime), openShiftDetails.KronosEndDateTime.Day > openShiftDetails.KronosStartDateTime.Day, Utility.OrgJobPathKronosConversion(openShiftOrgJobPath), openShiftDetails.DisplayName, openShiftDetails.KronosStartDateTime.TimeOfDay.ToString(), openShiftDetails.KronosEndDateTime.TimeOfDay.ToString(), openShift.SharedOpenShift.OpenSlotCount, comments).ConfigureAwait(false); if (creationResponse.Status != ApiConstants.Success) { return(ResponseHelper.CreateBadResponse(openShift.Id, error: "Open shift was not created successfully in Kronos.")); } var monthPartitionKey = Utility.GetMonthPartition( this.utility.FormatDateForKronos(openShiftDetails.KronosStartDateTime), this.utility.FormatDateForKronos(openShiftDetails.KronosEndDateTime)); await this.CreateAndStoreOpenShiftMapping(openShift, team, monthPartitionKey.FirstOrDefault(), openShiftOrgJobPath).ConfigureAwait(false); return(ResponseHelper.CreateSuccessfulResponse(openShift.Id)); }