diff options
| author | ivar <i@oiee.no> | 2025-10-19 23:41:23 +0200 |
|---|---|---|
| committer | ivar <i@oiee.no> | 2025-10-19 23:41:23 +0200 |
| commit | 3f4c0720e1e3421431e7baa20882a4a4512a7fab (patch) | |
| tree | 734ca81d7d0841d8863e3f523ebba14c282dc681 /src/Services | |
| download | fagprove-3f4c0720e1e3421431e7baa20882a4a4512a7fab.tar.xz fagprove-3f4c0720e1e3421431e7baa20882a4a4512a7fab.zip | |
Diffstat (limited to 'src/Services')
| -rw-r--r-- | src/Services/AppReservationService.cs | 76 | ||||
| -rw-r--r-- | src/Services/Interfaces/IAppReservationService.cs | 29 | ||||
| -rw-r--r-- | src/Services/Interfaces/IUserService.cs | 28 | ||||
| -rw-r--r-- | src/Services/UserService.cs | 77 |
4 files changed, 210 insertions, 0 deletions
diff --git a/src/Services/AppReservationService.cs b/src/Services/AppReservationService.cs new file mode 100644 index 0000000..2fc121b --- /dev/null +++ b/src/Services/AppReservationService.cs @@ -0,0 +1,76 @@ +using System; +using System.Linq; +using System.Text; +using IOL.Fagprove.Data; +using IOL.Fagprove.Data.Enums; +using IOL.Fagprove.Data.Models; +using IOL.Fagprove.Services.Interfaces; +using IOL.Fagprove.Utilities; + +namespace IOL.Fagprove.Services +{ + public class AppReservationService : IAppReservationService + { + private readonly AppDbContext _context; + + public AppReservationService(AppDbContext context) + { + _context = context; + } + + public bool ReservationIsPossible(Reservation reservation) + { + var reservationsForThisCabin = _context.Reservations.Where(r => + r.ReservationObjectId == reservation.ReservationObjectId + && r.Status == ReservationStatus.Granted + && DateTime.Compare(r.From, DateTime.Today) >= 0).ToList(); + if (reservationsForThisCabin.Count == 0) return true; + return reservationsForThisCabin.Select(item => item.From < reservation.To && reservation.From < item.To).All(overlaps => !overlaps); + } + + public bool ReservationIsValid(Reservation reservation) + { + if (reservation.From == default) return false; + if (reservation.To == default) return false; + if (reservation.UserId == default) return false; + if (reservation.ReservationObjectId == default) return false; + if (reservation.To < reservation.From) return false; + return reservation.From < reservation.To; + } + + public bool SendReservationStatusMail(Reservation reservation) + { + var user = _context.Users.SingleOrDefault(u => u.Id == reservation.UserId); + if (user == default) return false; + if (reservation.Status == ReservationStatus.Pending) return false; + var cabin = _context.Cabins.SingleOrDefault(c => c.Id == reservation.ReservationObjectId); + var message = new StringBuilder(); + var title = ""; + message.AppendLine("Hei " + user.Name); + message.AppendLine(); + switch (reservation.Status) + { + case ReservationStatus.Granted: + title = "Reservasjon innvilget"; + message.AppendLine("Din reservasjon er godkjent!"); + break; + case ReservationStatus.Rejected: + title = "Reservasjon avvist"; + message.AppendLine("Din reservasjon er avvist!"); + break; + } + message.AppendLine(); + message.AppendLine("Hytte: " + cabin?.Name); + message.AppendLine("Fra: " + reservation.From.ToString("dd-MM-yyyy")); + message.AppendLine("Til: " + reservation.To.ToString("dd-MM-yyyy")); + var statusMail = new Email + { + Message = message.ToString(), + Recepient = user.Email, + Title = title + " - Din bedrifts reservasjonstjeneste" + }; + statusMail.Send(); + return statusMail.Sent; + } + } +}
\ No newline at end of file diff --git a/src/Services/Interfaces/IAppReservationService.cs b/src/Services/Interfaces/IAppReservationService.cs new file mode 100644 index 0000000..cf41480 --- /dev/null +++ b/src/Services/Interfaces/IAppReservationService.cs @@ -0,0 +1,29 @@ +using System; +using System.Threading.Tasks; +using IOL.Fagprove.Data.Models; + +namespace IOL.Fagprove.Services.Interfaces +{ + public interface IAppReservationService + { + /// <summary> + /// Check current reservations to determine if provided reservation is possible to make. + /// </summary> + /// <param name="reservation">Reservation to check.</param> + /// <returns>Boolean indicating if the reservation is possible or not</returns> + bool ReservationIsPossible(Reservation reservation); + /// <summary> + /// Sends a status mail to the user that created provided reservation. + /// The contents of the message is determined by the provided reservation status. + /// </summary> + /// <param name="reservation">Reservation to send email on.</param> + /// <returns>Boolean indicating if the status mail was sent.</returns> + bool SendReservationStatusMail(Reservation reservation); + /// <summary> + /// Determines if the provided reservation is valid and can be saved to db. + /// </summary> + /// <param name="reservation">Reservation to check.</param> + /// <returns>Boolean indicating if the reservation is valid or not.</returns> + bool ReservationIsValid(Reservation reservation); + } +}
\ No newline at end of file diff --git a/src/Services/Interfaces/IUserService.cs b/src/Services/Interfaces/IUserService.cs new file mode 100644 index 0000000..f315c24 --- /dev/null +++ b/src/Services/Interfaces/IUserService.cs @@ -0,0 +1,28 @@ +using IOL.Fagprove.Data.Models; +using IOL.Fagprove.Data.DTOs; + +namespace IOL.Fagprove.Services.Interfaces +{ + public interface IUserService + { + /// <summary> + /// Sends welcome mail to the provided user. + /// </summary> + /// <param name="user">User to send mail to.</param> + /// <returns>Boolean indicating if the mail was sent or not.</returns> + bool SetTemporaryPasswordAndSendWelcomeMail(User user); + /// <summary> + /// Creates and sets a new temporary password on a user. + /// </summary> + /// <param name="user">User to work on.</param> + /// <returns>Returns the password or default if the task was unsuccesful.</returns> + bool SetNewTemporaryPasswordAndNotifyUser(User user); + /// <summary> + /// Updates the provided users password with the provided password. + /// </summary> + /// <param name="user">User to update.</param> + /// <param name="password">Unhashed new password.</param> + /// <returns>Boolean indicating if the new password was set and saved to the database.</returns> + bool UpdatePassword(User user, string password); + } +}
\ No newline at end of file diff --git a/src/Services/UserService.cs b/src/Services/UserService.cs new file mode 100644 index 0000000..6346f0b --- /dev/null +++ b/src/Services/UserService.cs @@ -0,0 +1,77 @@ +using System.Text; +using IOL.Fagprove.Data; +using IOL.Fagprove.Data.Models; +using IOL.Fagprove.Services.Interfaces; +using IOL.Fagprove.Utilities; + +namespace IOL.Fagprove.Services +{ + public class UserService : IUserService + { + private readonly AppDbContext _context; + + public UserService(AppDbContext context) + { + _context = context; + } + + public bool SetTemporaryPasswordAndSendWelcomeMail(User user) + { + var message = new StringBuilder(); + var temporaryPassword = SetNewTemporaryPassword(user); + message.AppendLine("Hei " + user.Name); + message.AppendLine("Du har fått en bruker i din bedrifts reservasjonstjeneste."); + message.AppendLine("Logg inn på ... med din e-postadresse og passord: " + temporaryPassword); + message.AppendLine("Vi oppfordrer deg til å skifte passord med en gang du har logget på."); + var welcomeEmail = new Email + { + Message = message.ToString(), + Recepient = user.Email, + Title = "Ny bruker - Din bedrifts reservasjonstjeneste" + }; + welcomeEmail.Send(); + return welcomeEmail.Sent; + } + + public bool SetNewTemporaryPasswordAndNotifyUser(User user) + { + var message = new StringBuilder(); + var temporaryPassword = SetNewTemporaryPassword(user); + message.AppendLine("Hei " + user.Name); + message.AppendLine("Du har fått ett midlertidig passord som du kan bruke til å sette nytt passord på din bruker."); + message.AppendLine("Logg inn på ... med din e-postadresse og passord: " + temporaryPassword); + message.AppendLine("Vi oppfordrer deg til å skifte passord med en gang du har logget på."); + var passwordEmail = new Email + { + Message = message.ToString(), + Recepient = user.Email, + Title = "Midlertidig passord - Din bedrifts reservasjonstjeneste" + }; + passwordEmail.Send(); + return passwordEmail.Sent; + } + + public bool UpdatePassword(User user, string password) + { + var passwordHash = PasswordHasher.HashPassword(password); + user.Password = passwordHash; + _context.Users.Update(user); + _context.Entry(user).Property(x => x.CreatedBy).IsModified = false; + _context.Entry(user).Property(x => x.CreatedUtc).IsModified = false; + _context.Entry(user).Property(x => x.Id).IsModified = false; + _context.SaveChanges(); + return true; + } + + private string SetNewTemporaryPassword(User user) + { + var temporaryPassword = Cryptography.RandomString(6); + var temporaryPasswordHash = PasswordHasher.HashPassword(temporaryPassword); + user.Password = temporaryPasswordHash; + _context.Users.Update(user); + _context.SaveChanges(); + return temporaryPassword; + } + } +} + |
