Uniconta get companies + convert to user type

Date: 2022-05-27
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Uniconta.API.System;
using Uniconta.Common;
using Uniconta.DataModel;

public static class UnicontaHelper
{
    public static T ConvertToUserType<T, U>(Company company, U oldType) where T : class, UnicontaBaseEntity, new() where U : class, UnicontaBaseEntity, new()
    {
        var correctType = company.CreateUserType<T>();
        StreamingManager.Copy(oldType, correctType);
        return correctType;
    }

    public static IEnumerable<T> ConvertToUserType<T, U>(Company company, IEnumerable<U> oldTypes) where T : class, U, new() where U : class, UnicontaBaseEntity, new()
    {
        return oldTypes.Select(oldType => ConvertToUserType<T, U>(company, oldType));
    }

    public static async Task<IEnumerable<CompanyClientUser>> GetCompanies(CrudAPI api)
    {
        return ConvertToUserType<CompanyClientUser, Company>(api.CompanyEntity, await api.session.GetCompanies());
    }
    
    public static CompanyClientUser CurrentCompany(CrudAPI api)
    {
        return ConvertToUserType<CompanyClientUser, Company>(api.CompanyEntity, api.CompanyEntity);
    }    
}
public static T ConvertToUserType<T, U>(Company company, U oldType) where T : class, UnicontaBaseEntity, new() where U : class, UnicontaBaseEntity, new()
{
    var correctType = company.CreateUserType<T>();
    StreamingManager.Copy(oldType, correctType);
    return correctType;
}

public static IEnumerable<T> LoadCache<T, U>(Company company, QueryAPI api) where T : class, UnicontaBaseEntity, IdKey, new() where U : class, UnicontaBaseEntity, new()
{
    var cache = ThreadsHelper.RunSync(() => api.LoadCache<T>());
    return cache.Cast<U>().Select(x => ConvertToUserType<T, U>(company, x)).ToList();
}

// usage:
var invItems = LoadCache<InvItemClientUser, InvItemClient>(api.CompanyEntity, api);
var emballageItemIds = invItems.Where(x => x.IsPackaging).Select(x => x.Item).ToHashSet();

62560cookie-checkUniconta get companies + convert to user type