public async Task <IActionResult> Post([FromBody] FirebaseModel model) { var fcm = _sender.Create(SenderFactory.FCM); var options = new FCMOption { Priority = "high", JsonData = model.JsonData, TargetScreen = model.TargetScreen }; var response = await fcm.SendAsync(model.DeviceToken, model.Title, model.Message, options); if (response.Successful) { return(Ok(response.Message)); } else { return(BadRequest(response.Message)); } }
public async Task <Response <HospitalAppointmentModel> > FinishAppointment(int id) { var response = InitErrorResponse(); var appt = await _unitOfWork.HospitalAppointmentRepository.GetSingleAsync(x => x.Id == id); if (appt == null) { response.Message = MessageConstant.NotFound; return(response); } if (await IsUserAssignToHospital(appt.HospitalId)) { if (appt.AppointmentStatus == AppointmentStatus.Process) { //update status appt.AppointmentStatus = AppointmentStatus.Finish; appt.UpdatedById = GetUserId(); appt.UpdatedDate = DateTime.UtcNow; appt.AppointmentFinished = DateTime.UtcNow; appt.UpdatedById = GetUserId(); _unitOfWork.HospitalAppointmentRepository.Edit(appt); _unitOfWork.Commit(); if (appt.UserId != null) { var userDeviceIds = await _unitOfWork.FirebaseUserMapRepository.FindByAsync(x => x.UserId == appt.UserId); ISender sender = _senderFact.Create(SenderFactory.FCM); var fcmPayload = GetFirebasePayload(); // ask user to fill doctor's review var medicalStaff = await _unitOfWork.MedicalStaffRepository.GetStaffWithHospitalDetailAsync(appt.MedicalStaffId, false); //generate json data var item = new { HospitalAppointmentId = appt.Id, Cover = medicalStaff.Images.Any()? medicalStaff.Images.First().Image.ImageUrl : "", medicalStaff.Title, medicalStaff.FirstName, medicalStaff.LastName, Specialist = medicalStaff.MedicalStaffSpecialists.Any()? medicalStaff.MedicalStaffSpecialists.FirstOrDefault().MedicalStaffSpecialist.Name : "" }; var option = new FCMOption { TargetScreen = NotificationModel.DOCTOR_RATING_REQUEST, JsonData = JsonConvert.SerializeObject(item, new JsonSerializerSettings() { PreserveReferencesHandling = PreserveReferencesHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.None, ContractResolver = new CamelCasePropertyNamesContractResolver() }), Priority = FCMOption.PriorityHigh }; var title = fcmPayload.Review.Title; var content = fcmPayload.Review.Content; await sender.SendMultipleAsync(userDeviceIds.Select(x => x.DeviceToken).ToList(), title, content, option); //save sending message to database _unitOfWork.NotificationRepository.Add(new Notification { Id = 0, TargetScreen = option.TargetScreen, JsonData = option.JsonData, Title = title, Message = content, ObjectId = appt.Id, UserId = appt.UserId ?? 0, IsRead = false, CreatedDate = DateTime.UtcNow, UpdatedDate = DateTime.UtcNow, CreatedById = GetUserId(), UpdatedById = GetUserId() }); _unitOfWork.Commit(); } } response = InitSuccessResponse(MessageConstant.Update); response.Item = GetModelFromEntity(appt); } else { response.Message = MessageConstant.UserNotAllowed; } return(response); }
// asyncronous method for sending appointment async Task <IList <Notification> > SendPendingAppointment(HospitalAppointment currentAppointment) { //get all pending appointment related to the current appointment var currentDate = DateTime.UtcNow; var pendingAppointment = await _unitOfWork.HospitalAppointmentRepository .FindByAsync(x => x.AppointmentStatus == AppointmentStatus.Pending && x.HospitalId == currentAppointment.HospitalId && x.MedicalStaffId == currentAppointment.MedicalStaffId && x.QueueNumber > currentAppointment.QueueNumber && x.AppointmentDate >= currentDate.StartOfDay() && x.AppointmentDate <= currentDate.EndOfDay()); //define setting var setting = await _unitOfWork.HospitalMedicalStaffRepository.GetSingleAsync(x => x.HospitalId == currentAppointment.HospitalId && x.MedicalStaffId == currentAppointment.MedicalStaffId, x => x.OperatingHours); //get detail staff and hospital var medicalStaff = await _unitOfWork.MedicalStaffRepository.GetStaffWithHospitalDetailAsync(currentAppointment.MedicalStaffId, false); var hospital = await _unitOfWork.HospitalRepository.GetSingleAsync(currentAppointment.HospitalId); var notifications = new List <Notification>(); // Convert PendingAppointment to PendingList Model // to make entities untracked, because if not // entities will doing arbitary insert var pendingList = GetModelFromEntity(pendingAppointment.ToList()); foreach (var appt in pendingList) { // just send pending appointment FCM // to registered user var item = GetEntityFromModel(appt); if (item.UserId != null) { //update estime time item.AppointmentDate = GetNextPatientEstimateTime(setting.EstimateTimeForPatient ?? 15, currentDate, item.QueueNumber); item.Hospital = hospital; item.MedicalStaff = medicalStaff; item.CurrentQueueNumber = currentAppointment.QueueNumber; var option = new FCMOption { TargetScreen = NotificationModel.APPOINTMENT_QUEUE_SCREEN, JsonData = JsonConvert.SerializeObject(item, new JsonSerializerSettings() { PreserveReferencesHandling = PreserveReferencesHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.None, ContractResolver = new CamelCasePropertyNamesContractResolver() }), Priority = FCMOption.PriorityHigh }; //send notification for all user devices var content = ContentGenerator.CurrentAppointment(GetModelFromEntity <HospitalAppointment, HospitalAppointmentModel>(item)); var userDeviceIds = await _unitOfWork.FirebaseUserMapRepository.FindByAsync(x => x.UserId == item.UserId); ISender sender = _senderFact.Create(SenderFactory.FCM); await sender.SendMultipleAsync(userDeviceIds.Select(x => x.DeviceToken).ToList(), content.Title, content.Body, option); notifications.Add(new Notification { Id = 0, TargetScreen = option.TargetScreen, JsonData = option.JsonData, Title = content.Title, Message = content.Body, ObjectId = item.Id, UserId = item.UserId ?? 0, IsRead = false, CreatedById = GetUserId(), UpdatedById = GetUserId(), CreatedDate = DateTime.UtcNow, UpdatedDate = DateTime.UtcNow }); } } return(notifications); }