public async Task <IHttpActionResult> PostOrder(OrderModel model, OrderCreationOptions opt = OrderCreationOptions.CREATE) { if (model == null) { return(BadRequest("No freaking payload man!")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var currentUserId = this.User.Identity.GetUserId(); if (!this.User.IsInRole(RoleNames.ROLE_ADMINISTRATOR) && !this.User.IsInRole(RoleNames.ROLE_BACKOFFICEADMIN)) { if (model.UserId != null && model.UserId != currentUserId) { logger.Error("INVALID OPERATION: Updating order/job id {0} is not authorized against user id {1}", model.UserId, this.User.Identity.GetUserId()); throw new InvalidOperationException($"Updating order/job id {model.UserId} is not authorized against user id {this.User.Identity.GetUserId()}"); } if (opt == OrderCreationOptions.CREATE_AND_CLAIM) { logger.Error("INVALID OPERATION: Claiming a job under user id {0} is not authorized", User.Identity.GetUserId()); throw new InvalidOperationException($"Claiming a job under user id {User.Identity.GetUserId()} is not authorized"); } } if (model.UserId == null) { model.UserId = currentUserId; } Job createdJob; var referenceUserForActivityLog = new ReferenceUser(currentUserId, this.User.Identity.GetUserName()); switch (opt) { case OrderCreationOptions.CREATE: createdJob = await repository.PostOrder(model); activitySubject.OnNext(new JobActivity(createdJob, JobActivityOperationNames.Create, referenceUserForActivityLog)); return(Ok(createdJob)); case OrderCreationOptions.CREATE_AND_CLAIM: createdJob = await repository.PostOrder(model, currentUserId); activitySubject.OnNext(new JobActivity(createdJob, JobActivityOperationNames.Create, referenceUserForActivityLog)); activitySubject.OnNext(new JobActivity(createdJob, JobActivityOperationNames.Claim, referenceUserForActivityLog)); return(Ok(createdJob)); default: logger.Error("Invalid OrderCreationOptions selected for order: {0}", model.Name); throw new InvalidOperationException("Invalid OrderCreationOptions selected"); } }
public async Task <IHttpActionResult> RestoreJob(string jobId) { if (string.IsNullOrWhiteSpace(jobId)) { logger.Error("Null or WhiteSpace in JobID: {0}", jobId); throw new ArgumentException(nameof(jobId)); } var currentUser = new ReferenceUser(this.User.Identity.GetUserId(), this.User.Identity.GetUserName()) { Name = this.User.Identity.GetUserFullName() }; var job = await repository.GetJob(jobId); var result = await repository.RestoreJob(job); Task.Factory.StartNew(() => { var activity = new JobActivity(job, JobActivityOperationNames.Restore, currentUser); activitySubject.OnNext(activity); }); return(Ok(result)); }
public async Task <IHttpActionResult> CancelJob([FromBody] JobCancellationRequest request) { if (request == null) { return(BadRequest("null request encountered")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var currentUser = new ReferenceUser(this.User.Identity.GetUserId(), this.User.Identity.GetUserName()) { Name = this.User.Identity.GetUserFullName() }; var job = await repository.GetJob(request.JobId); var result = await repository.CancelJob(job, request.Reason); Task.Factory.StartNew(() => { var activity = new JobActivity(job, JobActivityOperationNames.Cancel, currentUser); activitySubject.OnNext(activity); }); return(Ok(result)); }
public async Task <IHttpActionResult> UpdateOrder([FromUri] string jobId, [FromBody] OrderModel orderModel, [FromUri] string mode = JobUpdateMode.force) { if (!JobUpdateMode.IsValidUpdateMode(mode)) { throw new ArgumentException(nameof(mode)); } if (orderModel == null) { return(BadRequest("Null order payload provided")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var currentUserId = this.User.Identity.GetUserId(); var currentUser = new ReferenceUser(currentUserId, this.User.Identity.GetUserName()) { Name = this.User.Identity.GetUserFullName() }; var job = await repository.GetJob(jobId); if (!this.User.IsInRole(RoleNames.ROLE_ADMINISTRATOR) && !this.User.IsInRole(RoleNames.ROLE_BACKOFFICEADMIN) && !this.User.IsInRole(RoleNames.ROLE_ASSET)) { if (orderModel.UserId != null && orderModel.UserId != currentUserId) { logger.Error("Invalid Operation: Updating user id {0} is not authorized against user id {1}", orderModel.UserId, this.User.Identity.GetUserId()); throw new InvalidOperationException(string.Format( "Updating user id {0} is not authorized against user id {1}", orderModel.UserId, this.User.Identity.GetUserId())); } } else if (this.User.IsInRole(RoleNames.ROLE_ASSET) && !this.User.IsInRole(RoleNames.ROLE_ADMINISTRATOR) && !this.User.IsInRole(RoleNames.ROLE_BACKOFFICEADMIN)) { if (!job.Assets.Any(x => x.Key == currentUserId)) { logger.Error("{0} is not an associated asset with this job", currentUserId); throw new UnauthorizedAccessException($"{currentUserId} is not an associated asset with this job"); } } var result = await repository.UpdateOrder(job, orderModel, mode); Task.Factory.StartNew(() => { var activity = new JobActivity(job, JobActivityOperationNames.Update, nameof(Job.Order), currentUser); activitySubject.OnNext(activity); }); return(Ok(result)); }
private async Task <Data.Entity.Job> PostNewOrder(ClassifiedDeliveryOrder taskcatOrder) { var createdJob = await this.repository.PostOrder(taskcatOrder); var referenceUserForActivityLog = new ReferenceUser(taskcatOrder.UserId, createdJob.User.UserName); activitySubject.OnNext(new JobActivity(createdJob, JobActivityOperationNames.Create, referenceUserForActivityLog)); return(createdJob); }
public async Task <IHttpActionResult> Process(string jobid) { var job = await jobRepository.GetJob(jobid); var paymentMethod = service.GetPaymentMethodByKey(job.PaymentMethod); var result = paymentMethod.ProcessPayment(new ProcessPaymentRequest() { JobId = jobid }); var currentUser = new ReferenceUser(this.User.Identity.GetUserId(), this.User.Identity.GetUserName()) { Name = this.User.Identity.GetUserFullName() }; if (result.Errors != null && result.Errors.Count > 0) { return(Content(System.Net.HttpStatusCode.BadRequest, result.Errors)); } if (result.Success) { job.PaymentStatus = result.NewPaymentStatus; var jobUpdateResult = await jobRepository.UpdateJob(job); Task.Factory.StartNew(() => { var activity = new JobActivity(job, JobActivityOperationNames.Update, nameof(Job.PaymentStatus), currentUser) { Value = result.NewPaymentStatus.ToString() }; activitySubject.OnNext(activity); }); if (jobUpdateResult.ModifiedCount > 0) { return(Ok()); } else { logger.Error("Job update failed for JobId: {0}", jobid); throw new ServerErrorException("job update failed"); } } else { logger.Error("Internal Server Error"); return(InternalServerError()); } }
public async Task <IHttpActionResult> Tag([FromUri] string jobId, [FromBody] JsonPatchDocument <Job> tagPatch) { if (tagPatch == null) { throw new ArgumentException($"request body is null"); } if (!(User.IsInRole(RoleNames.ROLE_BACKOFFICEADMIN) || User.IsInRole(RoleNames.ROLE_ADMINISTRATOR))) { logger.Error("User {0} is not authorized to do this operation", User.Identity.Name); throw new UnauthorizedAccessException($"User {User.Identity.Name} is not authorized to do this operation"); } var unsupportedOp = tagPatch.Operations.Where(x => x.OperationType == OperationType.Move || x.OperationType == OperationType.Test || x.OperationType == OperationType.Copy); if (unsupportedOp.Count() > 0) { logger.Error($"Unsupported operation type {unsupportedOp.First()}"); throw new NotSupportedException($"Unsupported operation type {unsupportedOp.First()}"); } if (!tagPatch.Operations.All(x => x.path.StartsWith("/Tags/"))) { logger.Error("Patch operation not supported any fields except Tags"); throw new NotSupportedException("Patch operation not supported any fields except Tags"); } // Check tags meant for addition and replacements. var tagsToCheckFor = tagPatch.Operations.Where( x => x.OperationType == OperationType.Add || x.OperationType == OperationType.Replace) .Select(x => x.value.ToString()); // The question here might say why I didn't put this method in the repository // We don't really need this method to be reused, or at least don't see a potential // use case yet. When we do, we would make it a reusable method var nonExistentTags = tagsToCheckFor.Except( dataTagService.Collection.Find( Builders <DataTag> .Filter.Or(tagsToCheckFor.Select(x => Builders <DataTag> .Filter.Eq(y => y.Value, x)))) .ToList() .Select(x => x.Value)); if (nonExistentTags.Count() > 0) { throw new NotSupportedException($"tag {nonExistentTags.First()} is invalid"); } // get current user var currentUser = new ReferenceUser(this.User.Identity.GetUserId(), this.User.Identity.GetUserName()) { Name = this.User.Identity.GetUserFullName() }; var job = await repository.GetJob(jobId); job.Tags = job.Tags == null ? new List <string>() : job.Tags; tagPatch.ApplyTo(job); job.Tags = job.Tags.Distinct().ToList(); job.Tags = job.Tags.Count == 0 ? null : job.Tags; job.ModifiedTime = DateTime.UtcNow; // update job with tag var jobUpdateresult = await repository.UpdateJob(job); var result = new UpdateResult <Job>(jobUpdateresult.MatchedCount, jobUpdateresult.ModifiedCount, job); // log job activity var activity = new JobActivity(job, JobActivityOperationNames.Update, nameof(Job.Tags), currentUser); activitySubject.OnNext(activity); return(Ok(result)); }
public async Task <IHttpActionResult> Update([FromUri] string jobId, [FromUri] string taskId, [FromBody] JsonPatchDocument <JobTask> taskPatch, [FromUri] bool updatedValue = false) { if (taskPatch == null) { logger.Error($"{nameof(taskPatch)} is null"); throw new ArgumentNullException(nameof(taskPatch)); } if (taskPatch.Operations.Any(x => x.OperationType != Marvin.JsonPatch.Operations.OperationType.Replace)) { logger.Debug(taskPatch.Operations.ToString()); logger.Error("Operations except replace is not supported"); throw new NotSupportedException("Operations except replace is not supported"); } // INFO: This is ghetto, need to do it in a better way, may be write extension methods for JsonPatchDocument List <string> allowedPaths = new List <string>(); allowedPaths.Add(nameof(JobTask.AssetRef)); allowedPaths.Add(nameof(JobTask.State)); if (!taskPatch.Operations.All(x => allowedPaths.Any(a => x.path.EndsWith(a)))) { logger.Error("Patch operation not supported on one or more paths"); throw new NotSupportedException("Patch operation not supported on one or more paths"); } var currentUser = new ReferenceUser(this.User.Identity.GetUserId(), this.User.Identity.GetUserName()) { Name = this.User.Identity.GetUserFullName() }; var job = await repository.GetJob(jobId); var activities = new List <JobActivity>(); job.PropertyChanged += (sender, eventArgs) => { JobActivity jobChangeActivity = null; switch (eventArgs.PropertyName) { case nameof(Job.State): jobChangeActivity = new JobActivity(job, JobActivityOperationNames.Update, nameof(Job.State), currentUser) { Value = (sender as Job).State.ToString() }; activities.Add(jobChangeActivity); break; } }; var result = await repository.UpdateJobTaskWithPatch(job, taskId, taskPatch); result.SerializeUpdatedValue = updatedValue; var updatedTask = result.UpdatedValue.Tasks.First(x => x.id == taskId); var taskUpdates = new List <JobActivity>(); foreach (var op in taskPatch.Operations) { var taskActivity = new JobActivity( result.UpdatedValue, JobActivityOperationNames.Update, op.path.Substring(1), currentUser, new ReferenceActivity(taskId, updatedTask.Type)) { Value = op.value.ToString() }; taskUpdates.Add(taskActivity); } activities.InsertRange(0, taskUpdates); Task.Factory.StartNew(() => { foreach (var activity in activities) { activitySubject.OnNext(activity); } }); return(Ok(result)); }