shoc-backend/SeaHaven.Services/Implementation/CalendarService.cs
Alexandre Brandizzi 27c21ec32e fix(services): stamp user, contact, calendar and location audit times in UTC
User creation, contact, calendar event and site-contact create/modify/delete stamps used local server time. Validation rules comparing user-entered dates and the JWT expiry are unchanged.
2026-09-25 12:06:36 -03:00

125 lines
4.7 KiB
C#

using Data.SeaHavenIndustries;
using SeaHaven.DataServices.Interfaces;
using SeaHaven.Services.DTOs;
using SeaHaven.Services.Interfaces;
namespace SeaHaven.Services.Implementation
{
public class CalendarService : ICalendarService
{
private readonly ICalendarDataService _dataService;
public CalendarService(ICalendarDataService dataService)
{
_dataService = dataService;
}
public async Task CreateEventAsync(EditEventInput input, CancellationToken cancellationToken)
{
var model = new Events
{
Title = input.Title,
Description = input.Description,
Location = input.Location,
RecordID = input.RecordID,
RecordName = input.RecordName,
RecordTargetType = input.RecordTargetType,
ReminderMinutes = input.ReminderMinutes,
EventColor = input.EventColor,
StartDate = input.StartDate,
StartTime = input.StartTime,
EndDate = input.EndDate,
EndTime = input.EndTime,
AllDay = input.AllDay,
CreatedDate = DateTime.UtcNow,
IsDeleted = false
};
await _dataService.AddEventAsync(model, cancellationToken);
}
public async Task<bool> UpdateEventAsync(EditEventInput input, CancellationToken cancellationToken)
{
var model = await _dataService.GetEventForEditAsync(input.Id!.Value, cancellationToken);
if (model == null || model.IsDeleted == true)
return false;
model.Title = input.Title;
model.Description = input.Description;
model.Location = input.Location;
model.RecordID = input.RecordID;
model.RecordName = input.RecordName;
model.RecordTargetType = input.RecordTargetType;
model.ReminderMinutes = input.ReminderMinutes;
model.EventColor = input.EventColor;
model.StartDate = input.StartDate;
model.StartTime = input.StartTime;
model.EndDate = input.EndDate;
model.EndTime = input.EndTime;
model.AllDay = input.AllDay;
model.LastModificationTime = DateTime.UtcNow;
await _dataService.UpdateEventAsync(model, cancellationToken);
return true;
}
public async Task<EventDTO?> GetEventByIdAsync(int id, CancellationToken cancellationToken)
{
var model = await _dataService.GetEventByIdAsync(id, cancellationToken);
return model == null ? null : MapToDTO(model);
}
public async Task<bool> DeleteEventAsync(int id, CancellationToken cancellationToken)
{
var model = await _dataService.GetEventForEditAsync(id, cancellationToken);
if (model == null)
return false;
model.IsDeleted = true;
model.DeletionTime = DateTime.UtcNow;
await _dataService.UpdateEventAsync(model, cancellationToken);
return true;
}
public async Task<PagedEventsResult> GetEventListAsync(string? search, int page, int pageSize, CancellationToken cancellationToken)
{
var (items, totalCount) = await _dataService.GetEventListPagedAsync(page, pageSize, search, cancellationToken);
return new PagedEventsResult
{
Rows = items.Select(MapToDTO).ToList(),
TotalCount = totalCount
};
}
public async Task<IEnumerable<EventDTO>> GetEventsForCalendarAsync(DateTime? start, DateTime? end, CancellationToken cancellationToken)
{
var events = await _dataService.GetEventsForCalendarAsync(start, end, cancellationToken);
return events.Select(MapToDTO);
}
private static EventDTO MapToDTO(Events model)
{
return new EventDTO
{
Id = model.Id,
Title = model.Title,
Description = model.Description,
Location = model.Location,
RecordID = model.RecordID,
RecordName = model.RecordName,
RecordTargetType = model.RecordTargetType,
ReminderMinutes = model.ReminderMinutes,
EventColor = model.EventColor,
StartDate = model.StartDate,
StartTime = model.StartTime,
EndDate = model.EndDate,
EndTime = model.EndTime,
AllDay = model.AllDay,
CreatedDate = model.CreatedDate,
createdby = model.createdby
};
}
}
}