示例#1
0
        /// <summary>
        /// This method will enqueue active Fortnox invoices that have been
        ///   upserted in Fortnox since a given date to webCRM.
        /// </summary>
        internal async Task EnqueueRecentlyUpsertedInvoices(
            string storageAccountConnectionString,
            string webcrmSystemId,
            DateTime dateTimeSinceUpserted)
        {
            // Convert utc date we pass in to either CEST or CET.
            var dateTimeSinceUpsertedSwedish = dateTimeSinceUpserted.FromUtcToSwedish();

            var upsertedInvoices = await FortnoxClient.GetRecentlyUpsertedInvoices(dateTimeSinceUpsertedSwedish);

            Logger.LogInformation($"Found {upsertedInvoices.Count} Fortnox invoices to sync to webCRM that have been upserted since {dateTimeSinceUpsertedSwedish:yyyy-MM-dd HH:mm:ss}");

            var fortnoxQueue = await FortnoxQueue.Create(storageAccountConnectionString);

            foreach (var filteredInvoice in upsertedInvoices)
            {
                var payload = new FortnoxUpsertWebcrmDeliveryPayload
                {
                    FortnoxCustomerNumber = filteredInvoice.CustomerNumber,
                    FortnoxDocumentNumber = filteredInvoice.DocumentNumber,
                    WebcrmSystemId        = webcrmSystemId
                };
                string serializedPayload = JsonConvert.SerializeObject(payload);

                await fortnoxQueue.Enqueue(new FortnoxQueueMessage
                {
                    Action  = FortnoxQueueAction.UpsertFortnoxDelivery,
                    Payload = serializedPayload
                });
            }
        }
示例#2
0
        internal async Task CopyOrganisationToFortnox(OrganisationDto organisation, string organisationIdFieldName)
        {
            var customerNumber = organisation.GetPropertyValue(organisationIdFieldName);

            if (string.IsNullOrWhiteSpace(customerNumber))
            {
                var newFortnoxOrganisation = new Customer();
                CopyOrganisationProperties(organisation, newFortnoxOrganisation);
                var newCustomer = await FortnoxClient.CreateCustomer(newFortnoxOrganisation);

                organisation.SetPropertyValue(organisationIdFieldName, newCustomer.CustomerNumber);
                await WebcrmClient.UpdateOrganisation(organisation);
            }
            else
            {
                var customer = await FortnoxClient.GetCustomer(customerNumber);

                if (!customer.HasRelevantChanges(organisation))
                {
                    return;
                }

                CopyOrganisationProperties(organisation, customer);
                await FortnoxClient.UpdateCustomer(customer);
            }
        }
示例#3
0
        public static string GetCustomerComment(int ClientId)
        {
            FortnoxClient fnc = client();
            var           c   = GetCustomer(fnc, ClientId);

            return(c.Comments);
        }
示例#4
0
        internal async Task CopyOrganisationFromFortnox(string customerNumber, string organisationIdFieldName)
        {
            var customer = await FortnoxClient.GetCustomer(customerNumber);

            if (customer == null)
            {
                Logger.LogWarning($"Fortnox customer '{customerNumber}' could not be found.");
                return;
            }

            Logger.LogInformation($"Found and retrieved fortnox customer with customer number '{customer.CustomerNumber}'.");

            var correspondingWebcrmOrganisation = await WebcrmClient.GetOrganisationByField(organisationIdFieldName, customerNumber);

            if (correspondingWebcrmOrganisation == null)
            {
                var newWebcrmOrganisation = new OrganisationDto();
                CopyOrganisationProperties(customer, newWebcrmOrganisation, organisationIdFieldName);
                await WebcrmClient.CreateOrganisation(newWebcrmOrganisation);
            }
            else
            {
                if (!customer.HasRelevantChanges(correspondingWebcrmOrganisation))
                {
                    return;
                }

                CopyOrganisationProperties(customer, correspondingWebcrmOrganisation, organisationIdFieldName);
                await WebcrmClient.UpdateOrganisation(correspondingWebcrmOrganisation);
            }
        }
示例#5
0
        public static int Send(Order order)
        {
            FortnoxClient  fnc  = client();
            OrderConnector ocon = fnc.Get <OrderConnector>();

            ocon.Create(order);
            return(1);
        }
示例#6
0
 public FortnoxSynchroniser(TraceWriter logger,
                            string webcrmKey,
                            string fortnoxAccessToken,
                            string fortnoxClientSecret)
 {
     this.logger   = logger;
     fortnoxClient = new FortnoxClient(logger, fortnoxAccessToken, fortnoxClientSecret);
     webcrmClient  = new WebcrmClient(webcrmKey);
 }
示例#7
0
 private FortnoxDataCopier(
     ILogger logger,
     FortnoxApiKeys fortnoxApiKeys,
     WebcrmClient webcrmClient)
 {
     Logger        = logger;
     FortnoxClient = new FortnoxClient(fortnoxApiKeys);
     WebcrmClient  = webcrmClient;
 }
示例#8
0
    public async Task New_Sandbox()
    {
        var authorization = new StaticTokenAuth("6a5f9523-0f21-4488-801f-6094faba624d", "1Pevde6Pls");
        var client        = new FortnoxClient(authorization);

        var company = await client.CompanyInformationConnector.GetAsync();

        Assert.AreEqual("Richard-Sandbox-2022", company.CompanyName);
    }
示例#9
0
    public async Task TestConnection_WrongCredenials_Error()
    {
        //Arrange
        var auth          = new StaticTokenAuth("ABC", "DEF");
        var fortnoxClient = new FortnoxClient(auth);

        //Act
        var cc = fortnoxClient.CustomerConnector;
        await cc.FindAsync(null);
    }
示例#10
0
    public async Task Get()
    {
        var token         = "placeholder";
        var auth          = new StandardAuth(token);
        var fortnoxClient = new FortnoxClient(auth);

        var connector = fortnoxClient.ProfileConnector;
        var profile   = await connector.GetAsync();

        Assert.AreEqual("Richard Randak", profile.Name);
    }
示例#11
0
        public static int Send(List <Order> orders)
        {
            FortnoxClient  fnc  = client();
            OrderConnector ocon = fnc.Get <OrderConnector>();

            foreach (Order item in orders)
            {
                ocon.Create(item);
            }
            return(orders.Count);
        }
示例#12
0
    public async Task TestConnection_Credentials_Correct()
    {
        //Arrange
        var auth          = new StaticTokenAuth(TestCredentials.Access_Token, TestCredentials.Client_Secret);
        var fortnoxClient = new FortnoxClient(auth);

        //Act
        var connector = fortnoxClient.CustomerConnector;
        var customers = await connector.FindAsync(null);

        //Assert
        Assert.IsNotNull(customers);
    }
示例#13
0
    public void TestConnection_MultipleCredentials_Set()
    {
        var auth1          = new StaticTokenAuth("AT1", "CS1");
        var fortnoxClient1 = new FortnoxClient(auth1);

        var auth2          = new StaticTokenAuth("AT2", "CS2");
        var fortnoxClient2 = new FortnoxClient(auth2);

        var connector1 = fortnoxClient1.CustomerConnector;
        var connector2 = fortnoxClient2.CustomerConnector;

        Assert.AreEqual(auth1, connector1.Authorization);
        Assert.AreEqual(auth2, connector2.Authorization);
    }
示例#14
0
    public void TestConnection_Config_Set()
    {
        var auth       = new StaticTokenAuth("AccToken", "Secret");
        var httpClient = new HttpClient()
        {
            Timeout = TimeSpan.FromSeconds(10)
        };
        var fortnoxClient = new FortnoxClient(auth, httpClient, false);

        var connector = fortnoxClient.CustomerConnector;

        Assert.AreEqual(auth, connector.Authorization);
        Assert.AreEqual(false, connector.UseRateLimiter);
        Assert.AreEqual(10, connector.HttpClient.Timeout.Seconds);
    }
示例#15
0
        public static Customer GetCustomer(FortnoxClient fortnoxClient, int custId)
        {
            var      customerConnector = fortnoxClient.Get <CustomerConnector>();
            Customer customer          = new Customer();

            try
            {
                customer = customerConnector.Get(custId.ToString());
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
            return(customer);
        }
示例#16
0
        static FortnoxClient client()
        {
            FortnoxClient fortnoxClient = new FortnoxClient()
            {
                //SandBox AccessToken
                AccessToken = "8c688358-181e-4d6e-a0c1-7ba50618c32d",
                //AccessToken = AppSettings.ReadAppSetting("AccessToken"),   // prod
                ClientSecret = "PqQ6cN3Qk7",
                //ClientSecret = AppSettings.ReadAppSetting("ClientSecret"), // prod
                HttpClient = new HttpClient()
                {
                    Timeout = TimeSpan.FromSeconds(10)
                }
            };

            return(fortnoxClient);
        }
示例#17
0
        public void TestConnection_Using_FortnoxClient()
        {
            var fortnoxClient = new FortnoxClient()
            {
                AccessToken    = "AccToken",
                ClientSecret   = "Secret",
                UseRateLimiter = false,
                HttpClient     = new HttpClient()
                {
                    Timeout = TimeSpan.FromSeconds(10)
                }
            };

            var connector = fortnoxClient.Get <CustomerConnector>();

            Assert.AreEqual("AccToken", connector.AccessToken);
            Assert.AreEqual("Secret", connector.ClientSecret);
            Assert.AreEqual(false, connector.UseRateLimiter);
            Assert.AreEqual(10, connector.HttpClient.Timeout.Seconds);
        }
示例#18
0
        internal async Task EnqueueUpsertedInvoices(
            DateTime upsertedAfterUtc,
            FortnoxConfiguration configuration)
        {
            var fortnoxApiKeys = new FortnoxApiKeys(configuration.FortnoxAccessToken, configuration.FortnoxClientSecret);
            var fortnoxClient  = new FortnoxClient(fortnoxApiKeys);

            // Convert utc date we pass in to either CEST or CET.
            var dateTimeSinceUpsertedSwedish = upsertedAfterUtc.FromUtcToSwedish();

            var upsertedInvoices = await fortnoxClient.GetRecentlyUpsertedInvoices(dateTimeSinceUpsertedSwedish);

            Logger.LogInformation($"Found {upsertedInvoices.Count} upserted invoices in Fortnox upserted since {dateTimeSinceUpsertedSwedish:yyyy-MM-dd HH:mm:ss}.");

            foreach (var invoice in upsertedInvoices)
            {
                var payload = new UpsertDeliveryFromFortnoxPayload(invoice.CustomerNumber, invoice.DocumentNumber, configuration.WebcrmSystemId);

                await FortnoxQueue.Enqueue(new FortnoxQueueMessage(FortnoxQueueAction.UpsertFortnoxDelivery, payload));
            }
        }
示例#19
0
    public async Task Test_NoRateLimiter_TooManyRequest_Error()
    {
        var fortnoxClient = new FortnoxClient()
        {
            Authorization  = new StaticTokenAuth(TestCredentials.Access_Token, TestCredentials.Client_Secret),
            UseRateLimiter = false
        };

        var connector = fortnoxClient.CustomerConnector;

        FortnoxApiException error = null;
        int i;

        for (i = 0; i < 200; i++)
        {
            var searchSettings = new CustomerSearch();
            searchSettings.City = TestUtils.RandomString();
            try
            {
                await connector.FindAsync(searchSettings);
            }
            catch (FortnoxApiException ex)
            {
                error = ex;
                break;
            }
        }

        //Assert
        //Assert.IsTrue(failed > 0);
        Console.WriteLine($@"Succesful requests: {i}");
        Assert.IsNotNull(error);
        Console.WriteLine(error.Message);
        Assert.IsTrue(error.Message.Contains("Too Many Requests"));

        Thread.Sleep(5 * 1000); //Sleep to cooldown/recover from "debt" (otherwise following tests will fail with TooManyRequests)
    }
示例#20
0
 public FortnoxConnectorTester(ITestOutputHelper output) : base(output)
 {
     Client = new FortnoxClient(TestConfigurations.FortnoxApiKeys);
 }