using library_spo_utils.Enums; using library_spo_utils.Interfaces.Services; using Microsoft.Extensions.Logging; namespace library_spo_utils.Services; internal class PurchasingRequestBuildService : IPurchasingRequestBuildService { private readonly ISiteOptions _siteOptions; private readonly ISharePointAuthenticationManager _sharePointAuthenticationManager; private readonly ISharePointCustomOperation _spc; private readonly IPurchasingRequestDocLibraryService _purchasingRequestDocLibraryService; private readonly IPurchasingRequestDocSetService _purchasingRequestDocSetService; private readonly IFieldEntryDataUpdate _fieldEntryDataUpdate; private readonly ILogger _logger; public PurchasingRequestBuildService(ISiteOptions siteOptions, ISharePointAuthenticationManager sharePointAuthenticationManager, ISharePointCustomOperation spc, IPurchasingRequestDocLibraryService purchasingRequestDocLibraryService, IPurchasingRequestDocSetService purchasingRequestDocSetService, IFieldEntryDataUpdate fieldEntryDataUpdate, ILogger logger) { _siteOptions = siteOptions; _sharePointAuthenticationManager = sharePointAuthenticationManager; _spc = spc; _purchasingRequestDocLibraryService = purchasingRequestDocLibraryService; _purchasingRequestDocSetService = purchasingRequestDocSetService; _fieldEntryDataUpdate = fieldEntryDataUpdate; _logger = logger; } public void CreateIfNotExists(string purchasingRequestName) { var site = _siteOptions.GetPurchasingRequestSite(); var ctx = _sharePointAuthenticationManager.GetContext(site); var list = _siteOptions.GetPurchasingRequestLibrary(purchasingRequestName); var tenant = _siteOptions.GetPurchasingRequestTenat(); if (!_spc.SiteExist(ctx)) { throw new Exception($"Impossibile to build site it don't exist"); } if (!_spc.ListExist(ctx, list)) { _purchasingRequestDocLibraryService.Create(list, ctx); } if (!_spc.FolderExistsInsideList(ctx, list, purchasingRequestName)) { _purchasingRequestDocSetService.Create(purchasingRequestName, list, tenant, ctx); } else { _logger.LogInformation($"Quotation with {purchasingRequestName} already exist"); _fieldEntryDataUpdate.FieldUpdate(ctx, list, purchasingRequestName, FieldUpdateType.PurchasingRequest); } } }