Пример #1
0
        public static async Task ArchiveTeamAsync(FlightTeam team)
        {
            var crew = await graphClient.GetUserIds(team.Pilots, team.FlightAttendants);

            // Remove event from crew calendars
            await TeamProvisioning.RemoveFlightFromCalendars(crew, team.FlightNumber);

            // Archive team
            try
            {
                await graphClient.ArchiveTeamAsync(team.TeamId);
            }
            catch (ServiceException ex)
            {
                logger.LogInformation($"Attempt to archive team failed: {ex.Message}");
            }
        }
Пример #2
0
        public static async Task UpdateTeamAsync(FlightTeam originalTeam, FlightTeam updatedTeam)
        {
            // Look for changes that require an update via Graph
            // Did the admin change?
            var admin = await graphClient.GetUserByUpn(updatedTeam.Admin);

            updatedTeam.Admin = admin.Id;
            if (!admin.Id.Equals(originalTeam.Admin))
            {
                // Add new owner
                await graphClient.AddMemberAsync(originalTeam.TeamId, admin.Id, true);

                // Remove old owner
                await graphClient.RemoveMemberAsync(originalTeam.TeamId, admin.Id, true);
            }

            bool isCrewChanged = false;

            // Add new pilots
            var newPilots = updatedTeam.Pilots.Except(originalTeam.Pilots);

            foreach (var pilot in newPilots)
            {
                isCrewChanged = true;
                var pilotUser = await graphClient.GetUserByUpn(pilot);

                await graphClient.AddMemberAsync(originalTeam.TeamId, pilotUser.Id);
            }

            if (newPilots.Count() > 0)
            {
                await TeamProvisioning.AddFlightToCalendars(updatedTeam, newPilots.ToList());
            }

            // Remove any removed pilots
            var removedPilots = originalTeam.Pilots.Except(updatedTeam.Pilots);

            foreach (var pilot in removedPilots)
            {
                isCrewChanged = true;
                var pilotUser = await graphClient.GetUserByUpn(pilot);

                await graphClient.RemoveMemberAsync(originalTeam.TeamId, pilotUser.Id);
            }

            if (removedPilots.Count() > 0)
            {
                await TeamProvisioning.RemoveFlightFromCalendars(removedPilots.ToList(), updatedTeam.FlightNumber);
            }

            // Add new flight attendants
            var newFlightAttendants = updatedTeam.FlightAttendants.Except(originalTeam.FlightAttendants);

            foreach (var attendant in newFlightAttendants)
            {
                isCrewChanged = true;
                var attendantUser = await graphClient.GetUserByUpn(attendant);

                await graphClient.AddMemberAsync(originalTeam.TeamId, attendantUser.Id);
            }

            if (newFlightAttendants.Count() > 0)
            {
                await TeamProvisioning.AddFlightToCalendars(updatedTeam, newFlightAttendants.ToList());
            }

            // Remove any removed flight attendants
            var removedFlightAttendants = originalTeam.FlightAttendants.Except(updatedTeam.FlightAttendants);

            foreach (var attendant in removedFlightAttendants)
            {
                isCrewChanged = true;
                var attendantUser = await graphClient.GetUserByUpn(attendant);

                await graphClient.RemoveMemberAsync(originalTeam.TeamId, attendantUser.Id);
            }

            if (removedFlightAttendants.Count() > 0)
            {
                await TeamProvisioning.RemoveFlightFromCalendars(removedFlightAttendants.ToList(), updatedTeam.FlightNumber);
            }

            // Swap out catering liaison if needed
            if (updatedTeam.CateringLiaison != null &&
                !updatedTeam.CateringLiaison.Equals(originalTeam.CateringLiaison))
            {
                var oldCateringLiaison = await graphClient.GetUserByEmail(originalTeam.CateringLiaison);

                await graphClient.RemoveMemberAsync(originalTeam.TeamId, oldCateringLiaison.Id);
                await AddGuestUser(originalTeam.TeamId, updatedTeam.CateringLiaison);
            }

            // Check for changes to gate, time
            bool isGateChanged          = updatedTeam.DepartureGate != originalTeam.DepartureGate;
            bool isDepartureTimeChanged = updatedTeam.DepartureTime != originalTeam.DepartureTime;

            List <string> crew    = null;
            string        newGate = null;

            if (isCrewChanged || isGateChanged || isDepartureTimeChanged)
            {
                crew = await graphClient.GetUserIds(updatedTeam.Pilots, updatedTeam.FlightAttendants);

                newGate = isGateChanged ? updatedTeam.DepartureGate : null;

                logger.LogInformation("Updating flight in crew members' calendars");

                if (isDepartureTimeChanged)
                {
                    await TeamProvisioning.UpdateFlightInCalendars(crew, updatedTeam.FlightNumber, updatedTeam.DepartureGate, updatedTeam.DepartureTime);
                }
                else
                {
                    await TeamProvisioning.UpdateFlightInCalendars(crew, updatedTeam.FlightNumber, updatedTeam.DepartureGate);
                }
            }

            if (isGateChanged || isDepartureTimeChanged)
            {
                var    localTimeZone    = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
                string newDepartureTime = isDepartureTimeChanged ? TimeZoneInfo.ConvertTime(updatedTeam.DepartureTime, localTimeZone).ToString("g") : null;

                logger.LogInformation("Sending notification to crew members' devices");
                await TeamProvisioning.SendDeviceNotifications(crew, updatedTeam.FlightNumber,
                                                               newGate, newDepartureTime);
            }
        }