/// <summary> /// Delete or Reactivate a inventory /// </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_Inventory").Name; // Get MongoDB var db = context.Database; var inventoryCollection = db.GetCollection <Deathstar.Data.Models.PC_Inventory>(collectionName); // Filter var filters = Builders <Deathstar.Data.Models.PC_Inventory> .Filter.Eq(x => x.Id, id); filters = filters & Builders <Deathstar.Data.Models.PC_Inventory> .Filter.Eq(x => x.DV_CompanyId, companyId.ToString()); // Timestamp string timestamp = DateTimeOffset.Now.ToString("yyyy/MM/dd HH:mm:ss.fff zzz"); var dbInventory = inventoryCollection.Find(filters).FirstOrDefault(); if (dbInventory == null) { string reason = $"Inventory not found by Id:{id} provided."; IG2000.Data.Utilities.Logging.LogTrackingEvent($"Inventory 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 }; } // Update the Following Fields var update = Builders <Deathstar.Data.Models.PC_Inventory> .Update .Set(x => x.IsActive, reactivate) .Set(x => x.UpdatedBy, context.Security.GetEmail()) .Set(x => x.UpdatedDateTime, timestamp); // Update database record await inventoryCollection.UpdateOneAsync(filters, update); // Update the corresponding External Id record(s) await _externalIdModel.DeleteOrReactivateByParentId(dbInventory.Id.ToString(), reactivate, dbInventory.ST_TableName, timestamp, dbInventory.UpdatedDateTime, trackingGuid, ""); return(Microsoft.AspNetCore.Http.StatusCodes.Status204NoContent); }
/// <summary> /// Delete or reactivate an existing category /// </summary> /// <param name="id"></param> /// <param name="reactivate"></param> /// <param name="context"></param> /// <param name="trackingGuid"></param> /// <returns></returns> public async Task <int> DeleteOrReactivate(IBaseContextModel context, 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_Category").Name; string productCollectionName = database.Collections.FirstOrDefault(x => x.Name == "PC_Product").Name; string variantCollectionName = database.Collections.FirstOrDefault(x => x.Name == "PC_ProductVariant").Name; // Get MongoDB var db = context.Database; //var categoryCollection = db.GetCollection<Deathstar.Data.Models.PC_Category>(collectionName); // Filter var filters = Builders <Deathstar.Data.Models.PC_Category> .Filter.Eq(x => x.Id, id); filters = filters & Builders <Deathstar.Data.Models.PC_Category> .Filter.Eq(x => x.DV_CompanyId, companyId.ToString()); var dbCategory = _categoryCollection.Find(filters).FirstOrDefault(); if (dbCategory == null) { string reason = $"Category not found by Id:{id} provided."; IG2000.Data.Utilities.Logging.LogTrackingEvent($"Category 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_Category> .Update .Set(x => x.IsActive, reactivate) .Set(x => x.UpdatedBy, context.Security.GetEmail()) .Set(x => x.UpdatedDateTime, timestamp); // Update database record await _categoryCollection.UpdateOneAsync(filters, update); // Remove all category assignments only on delete if (!reactivate) { // Remove all category assignments var productCollection = db.GetCollection <Deathstar.Data.Models.PC_Product>(productCollectionName); var variantCollection = db.GetCollection <Deathstar.Data.Models.PC_ProductVariant>(variantCollectionName); // Find all Products Filter var productFilters = Builders <Deathstar.Data.Models.PC_Product> .Filter.Eq(x => x.DV_CompanyId, companyId.ToString()); productFilters = productFilters & Builders <Deathstar.Data.Models.PC_Product> .Filter.ElemMatch(x => x.Categories, x => x.CategoryId == id); // Find all the products that have this category assignment var dbProducts = productCollection.FindAsync(productFilters).Result.ToList(); // get a list of all the category assignments that match the categroy id var dbProductCategoryAssignments = dbProducts.SelectMany(x => x.Categories).Where(y => y.CategoryId == id).ToList(); foreach (var dbProductCategoryAssignment in dbProductCategoryAssignments) { //circular reference await _categoryAssignment.DeleteOrReactivate(context, dbProductCategoryAssignment.PC_ProductCategory_Id, "PC_Product", false, trackingGuid); } // Variant Filter var variantFilters = Builders <Deathstar.Data.Models.PC_ProductVariant> .Filter.Eq(x => x.DV_CompanyId, companyId.ToString()); variantFilters = variantFilters & Builders <Deathstar.Data.Models.PC_ProductVariant> .Filter.ElemMatch(x => x.Categories, x => x.CategoryId == id); // Find all the variants that have this category assignment var dbVariants = variantCollection.FindAsync(variantFilters).Result.ToList(); // get a list of all the category assignments that match the category id var dbVariantCategoryAssignments = dbVariants.SelectMany(x => x.Categories).Where(y => y.CategoryId == id).ToList(); foreach (var dbVariantCategoryAssignment in dbVariantCategoryAssignments) { //circular reference await _categoryAssignment.DeleteOrReactivate(context, dbVariantCategoryAssignment.PC_ProductCategory_Id, "PC_ProductVariant", false, trackingGuid); } } else // if reactivating send update webhook { // Build the Webhook event var whRequest = new Sheev.Common.Models.WebhookResponse() { CompanyId = companyId.ToString(), Type = "Product Category", Scope = $"category/product/updated", Id = id.ToString(), TrackingGuid = trackingGuid }; // Trigger the Webhook event await _webHookModel.FireWebhookEvent(whRequest, context, trackingGuid); } // Update the corresponding External Id record(s) await _externalIdModel.DeleteOrReactivateByParentId(dbCategory.Id.ToString(), reactivate, "PC_Category", timestamp, dbCategory.UpdatedDateTime, trackingGuid, "category/product/deleted", true); return(Microsoft.AspNetCore.Http.StatusCodes.Status204NoContent); }
/// <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); }