public async Task <NoContentResult> ReactivateProduct(string id, [FromQuery] Guid?trackingGuid = null) { _context.Security = new K2SO.Auth.Security(HttpContext.Request.Headers["Authorization"]); trackingGuid = IG2000.Data.Utilities.Logging.CreateLogTrackingHeader(trackingGuid, _context); long longId = Utilities.RestErrorHandler.CheckId(id, _context, (Guid)trackingGuid); await _inventoryModel.DeleteOrReactivate(longId, true, (Guid)trackingGuid); return(NoContent()); }
/// <summary> /// Delete or reactivate a product /// </summary> /// <param name="id"></param> /// <param name="reactivate"></param> /// <param name="context"></param> /// <param name="trackingGuid"></param> /// <returns></returns> public async Task <int> DeleteOrReactivate(long id, bool reactivate, Guid trackingGuid) { var companyId = context.Security.GetCompanyId(); // MongoDB Settings var database = context.MongoDbSettings.Value.Databases.FirstOrDefault(x => x.Name == "DeathStar"); string collectionName = database.Collections.FirstOrDefault(x => x.Name == "PC_Product").Name; // Get MongoDB var db = context.Database; var productCollection = db.GetCollection <Deathstar.Data.Models.PC_Product>(collectionName); // Filters - note always start with the company id var filters = Builders <Deathstar.Data.Models.PC_Product> .Filter.Eq(x => x.DV_CompanyId, companyId.ToString()); filters = filters & Builders <Deathstar.Data.Models.PC_Product> .Filter.Eq(x => x.Id, id); //IG2000.Data.Utilities.Logging.LogTrackingEvent($"Beginning get product by Id by user: {context.Security.GetEmail()},", "Get Product (MongoDB)", LT319.Common.Utilities.Constants.TrackingStatus.Active, trackingGuid); var dbProduct = productCollection.Find(filters).FirstOrDefault(); if (dbProduct == null) { string reason = $"Product not found by Id:{id} provided."; IG2000.Data.Utilities.Logging.LogTrackingEvent($"Product was unable to be retrieved! Reason: {reason}", $"Status Code: {Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound}", LT319.Common.Utilities.Constants.TrackingStatus.Error, context, trackingGuid); throw new HttpResponseException() { StatusCode = Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound, ReasonPhrase = reason }; } // Set the updated timestamp here string timestamp = DateTimeOffset.Now.ToString("yyyy/MM/dd HH:mm:ss.fff zzz"); // Update the Following Fields var update = Builders <Deathstar.Data.Models.PC_Product> .Update .Set(x => x.IsActive, reactivate) .Set(x => x.UpdatedBy, context.Security.GetEmail()) .Set(x => x.UpdatedDateTime, timestamp); // Update database record await productCollection.UpdateOneAsync(filters, update); // Update the corresponding Inventory record await _inventoryModel.DeleteOrReactivate(dbProduct.Id, reactivate, "PC_Product", timestamp, dbProduct.UpdatedDateTime, trackingGuid); // Update the corresponding Variant record(s) await _variantModel.DeleteOrReactivateByProductId(dbProduct.Id, reactivate, timestamp, dbProduct.UpdatedDateTime, trackingGuid); // Update the corresponding External Id record(s) await _externalIdModel.DeleteOrReactivateByParentId(dbProduct.Id.ToString(), reactivate, "PC_Product", timestamp, dbProduct.UpdatedDateTime, trackingGuid, ""); // determine if the hook should be a delete or update string whScope = reactivate ? "updated" : "deleted"; // Build the Webhook event var whRequest = new Sheev.Common.Models.WebhookResponse() { CompanyId = companyId.ToString(), Type = "Product", Scope = $"product/{whScope}", Id = id.ToString() }; // Trigger the Webhook event await _webHookModel.FireWebhookEvent(whRequest, context, trackingGuid); return(Microsoft.AspNetCore.Http.StatusCodes.Status204NoContent); }