feat: update database schema and add export functionality for users to Fatture In Cloud
This commit is contained in:
parent
5bff7219e8
commit
2f9a0762ad
8 changed files with 560 additions and 10 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
|
@ -24,4 +24,8 @@ apps/infoalloggi/npm-debug.log*
|
|||
apps/infoalloggi/.env
|
||||
apps/infoalloggi/.env*.local
|
||||
apps/infoalloggi/*.tsbuildinfo
|
||||
apps/infoalloggi/certificates
|
||||
apps/infoalloggi/certificates
|
||||
apps/infoalloggi/fic_cities.json
|
||||
apps/infoalloggi/fic_nations.json
|
||||
apps/infoalloggi/gi_comuni_cap.json
|
||||
apps/infoalloggi/gi_nazioni.json
|
||||
|
|
|
|||
21
apps/db/migrations/41_catasto2.up.sql
Normal file
21
apps/db/migrations/41_catasto2.up.sql
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
ALTER TABLE IF EXISTS public.nazioni
|
||||
ADD COLUMN IF NOT EXISTS iso TEXT;
|
||||
|
||||
|
||||
UPDATE public.nazioni
|
||||
SET
|
||||
nome = v.new_nome
|
||||
FROM (
|
||||
VALUES ('Z204', 'BAHRAIN'), ('Z153', 'BOSNIA ED ERZEGOVINA'), ('Z140', 'MOLDAVIA'), ('Z518', 'PORTO RICO')
|
||||
) AS v (codice_belfiore, new_nome)
|
||||
WHERE
|
||||
nazioni.catasto = v.codice_belfiore;
|
||||
|
||||
UPDATE public.nazioni
|
||||
SET
|
||||
iso = DATA.iso
|
||||
FROM (
|
||||
VALUES ('Z200', 'AF'), ('Z100', 'AL'), ('Z301', 'DZ'), ('Z101', 'AD'), ('Z302', 'AO'), ('Z529', 'AI'), ('Z532', 'AG'), ('Z501', 'AN'), ('Z203', 'SA'), ('Z600', 'AR'), ('Z252', 'AM'), ('Z700', 'AU'), ('Z102', 'AT'), ('Z253', 'AZ'), ('Z502', 'BS'), ('Z204', 'BH'), ('Z249', 'BD'), ('Z522', 'BB'), ('Z103', 'BE'), ('Z512', 'BZ'), ('Z314', 'BJ'), ('Z400', 'BM'), ('Z205', 'BT'), ('Z139', 'BY'), ('Z601', 'BO'), ('Z153', 'BA'), ('Z358', 'BW'), ('Z602', 'BR'), ('Z207', 'BN'), ('Z104', 'BG'), ('Z354', 'BF'), ('Z305', 'BI'), ('Z208', 'KH'), ('Z306', 'CM'), ('Z401', 'CA'), ('Z307', 'CV'), ('Z309', 'TD'), ('Z603', 'CL'), ('Z210', 'CN'), ('Z211', 'CY'), ('Z604', 'CO'), ('Z310', 'KM'), ('Z503', 'CR'), ('Z313', 'CI'), ('Z149', 'HR'), ('Z504', 'CU'), ('Z107', 'DK'), ('Z526', 'DM'), ('Z605', 'EC'), ('Z336', 'EG'), ('Z506', 'SV'), ('Z215', 'AE'), ('Z368', 'ER'), ('Z144', 'EE'), ('Z315', 'ET'), ('Z216', 'PH'), ('Z109', 'FI'), ('Z110', 'FR'), ('Z316', 'GA'), ('Z317', 'GM'), ('Z254', 'GE'), ('Z112', 'DE'), ('Z318', 'GH'), ('Z507', 'JM'), ('Z219', 'JP'), ('Z113', 'GI'), ('Z361', 'DJ'), ('Z220', 'JO'), ('Z115', 'GR'), ('Z524', 'GD'), ('Z402', 'GL'), ('Z508', 'GP'), ('Z509', 'GT'), ('Z319', 'GN'), ('Z321', 'GQ'), ('Z606', 'GY'), ('Z607', 'GF'), ('Z510', 'HT'), ('Z511', 'HN'), ('Z222', 'IN'), ('Z223', 'ID'), ('Z224', 'IR'), ('Z225', 'IQ'), ('Z116', 'IE'), ('Z117', 'IS'), ('Z122', 'IM'), ('Z530', 'KY'), ('Z703', 'CK'), ('Z711', 'MH'), ('Z722', 'PN'), ('Z724', 'SB'), ('Z525', 'VG'), ('Z226', 'IL'), ('Z255', 'KZ'), ('Z322', 'KE'), ('Z256', 'KG'), ('Z731', 'KI'), ('Z160', 'XK'), ('Z227', 'KW'), ('Z228', 'LA'), ('Z359', 'LS'), ('Z145', 'LV'), ('Z229', 'LB'), ('Z325', 'LR'), ('Z326', 'LY'), ('Z119', 'LI'), ('Z146', 'LT'), ('Z120', 'LU'), ('Z231', 'MO'), ('Z148', 'MK'), ('Z327', 'MG'), ('Z328', 'MW'), ('Z247', 'MY'), ('Z232', 'MV'), ('Z329', 'ML'), ('Z121', 'MT'), ('Z330', 'MA'), ('Z513', 'MQ'), ('Z331', 'MR'), ('Z332', 'MU'), ('Z360', 'YT'), ('Z514', 'MX'), ('Z140', 'MD'), ('Z123', 'MC'), ('Z233', 'MN'), ('Z159', 'ME'), ('Z531', 'MS'), ('Z333', 'MZ'), ('Z206', 'MM'), ('Z300', 'NA'), ('Z713', 'NR'), ('Z234', 'NP'), ('Z515', 'NI'), ('Z334', 'NE'), ('Z335', 'NG'), ('Z714', 'NU'), ('Z125', 'NO'), ('Z716', 'NC'), ('Z719', 'NZ'), ('Z235', 'OM'), ('Z126', 'NL'), ('Z236', 'PK'), ('Z734', 'PW'), ('Z516', 'PA'), ('Z730', 'PG'), ('Z610', 'PY'), ('Z611', 'PE'), ('Z127', 'PL'), ('Z518', 'PR'), ('Z128', 'PT'), ('Z237', 'QA'), ('Z114', 'GB'), ('Z156', 'CZ'), ('Z308', 'CF'), ('Z312', 'CD'), ('Z505', 'DO'), ('Z324', 'RE'), ('Z129', 'RO'), ('Z338', 'RW'), ('Z154', 'RU'), ('Z533', 'KN'), ('Z403', 'PM'), ('Z528', 'VC'), ('Z726', 'WS'), ('Z130', 'SM'), ('Z340', 'SH'), ('Z343', 'SN'), ('Z158', 'RS'), ('Z342', 'SC'), ('Z344', 'SL'), ('Z248', 'SG'), ('Z240', 'SY'), ('Z155', 'SK'), ('Z150', 'SI'), ('Z345', 'SO'), ('Z131', 'ES'), ('Z209', 'LK'), ('Z404', 'US'), ('Z907', 'SS'), ('Z348', 'SD'), ('Z608', 'SR'), ('Z132', 'SE'), ('Z133', 'CH'), ('Z257', 'TJ'), ('Z217', 'TW'), ('Z357', 'TZ'), ('Z241', 'TH'), ('Z351', 'TG'), ('Z728', 'TO'), ('Z612', 'TT'), ('Z352', 'TN'), ('Z243', 'TR'), ('Z258', 'TM'), ('Z732', 'TV'), ('Z138', 'UA'), ('Z353', 'UG'), ('Z134', 'HU'), ('Z613', 'UY'), ('Z259', 'UZ'), ('Z733', 'VU'), ('Z614', 'VE'), ('Z251', 'VN'), ('Z246', 'YE'), ('Z355', 'ZM'), ('Z337', 'ZW')
|
||||
) AS DATA (catasto, iso)
|
||||
WHERE
|
||||
nazioni.catasto = DATA.catasto;
|
||||
|
|
@ -232,7 +232,10 @@ const EditUser: NextPageWithLayout<EditUserProps> = ({
|
|||
</TabsContent>
|
||||
<TabsContent className="space-y-6" value="fatture">
|
||||
{userData.codice_fiscale ? (
|
||||
<FatturazioneData cf={userData.codice_fiscale} />
|
||||
<FatturazioneData
|
||||
cf={userData.codice_fiscale}
|
||||
userId={userData.id}
|
||||
/>
|
||||
) : (
|
||||
<p>Codice Fiscale non disponibile per questo utente.</p>
|
||||
)}
|
||||
|
|
@ -287,10 +290,24 @@ EditUser.getLayout = function getLayout(page) {
|
|||
return <AreaRiservataLayoutUserView>{page}</AreaRiservataLayoutUserView>;
|
||||
};
|
||||
|
||||
const FatturazioneData = ({ cf }: { cf: string }) => {
|
||||
const FatturazioneData = ({ userId, cf }: { userId: UsersId; cf: string }) => {
|
||||
const { data, isLoading } = api.fatture.getClientFromCF.useQuery({
|
||||
cf,
|
||||
});
|
||||
const utils = api.useUtils();
|
||||
const { mutate: export_mutate } = api.fatture.addToFIC.useMutation({
|
||||
onSuccess: async () => {
|
||||
await utils.users.getClientProfilo.invalidate({
|
||||
userId,
|
||||
});
|
||||
toast.success("Utente esportato con successo su Fatture In Cloud");
|
||||
},
|
||||
onError: (error) => {
|
||||
toast.error(
|
||||
`Errore durante l'esportazione su Fatture In Cloud: ${error.message}`,
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
if (isLoading) return <LoadingPage />;
|
||||
if (!data)
|
||||
|
|
@ -318,7 +335,19 @@ const FatturazioneData = ({ cf }: { cf: string }) => {
|
|||
</div>
|
||||
))
|
||||
) : (
|
||||
<span>Nessun dato trovato per il codice fiscale {cf}</span>
|
||||
<div className="space-y-4">
|
||||
<p>Nessun dato trovato per il codice fiscale {cf}</p>
|
||||
|
||||
<Button
|
||||
onClick={() => {
|
||||
export_mutate({
|
||||
userId,
|
||||
});
|
||||
}}
|
||||
>
|
||||
Esporta su Fatture In Cloud
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ export default interface NazioniTable {
|
|||
nome: ColumnType<string, string, string>;
|
||||
|
||||
catasto: ColumnType<string, string, string>;
|
||||
|
||||
iso: ColumnType<string | null, string | null, string | null>;
|
||||
}
|
||||
|
||||
export type Nazioni = Selectable<NazioniTable>;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import {
|
||||
ClientsApi,
|
||||
Configuration,
|
||||
InfoApi,
|
||||
} from "@fattureincloud/fattureincloud-ts-sdk";
|
||||
import { env } from "~/env";
|
||||
|
||||
|
|
@ -10,3 +11,259 @@ const apiConfig = new Configuration({
|
|||
|
||||
export const companyId = Number.parseInt(env.FIC_COMPANY_ID);
|
||||
export const clientsApi = new ClientsApi(apiConfig);
|
||||
|
||||
export const infosApi = new InfoApi(apiConfig);
|
||||
/*
|
||||
const FIC_COUNTRIES = [
|
||||
"Italia",
|
||||
"Afghanistan",
|
||||
"Albania",
|
||||
"Algeria",
|
||||
"Andorra",
|
||||
"Angola",
|
||||
"Anguilla",
|
||||
"Antartide",
|
||||
"Antigua e Barbuda",
|
||||
"Antille Olandesi",
|
||||
"Arabia Saudita",
|
||||
"Argentina",
|
||||
"Armenia",
|
||||
"Aruba",
|
||||
"Australia",
|
||||
"Austria",
|
||||
"Azerbaigian",
|
||||
"Bahamas",
|
||||
"Bahrain",
|
||||
"Bangladesh",
|
||||
"Barbados",
|
||||
"Belgio",
|
||||
"Belize",
|
||||
"Benin",
|
||||
"Bermuda",
|
||||
"Bhutan",
|
||||
"Bielorussia",
|
||||
"Bolivia",
|
||||
"Bosnia ed Erzegovina",
|
||||
"Botswana",
|
||||
"Brasile",
|
||||
"Brunei",
|
||||
"Bulgaria",
|
||||
"Burkina Faso",
|
||||
"Burundi",
|
||||
"Cambogia",
|
||||
"Camerun",
|
||||
"Canada",
|
||||
"Capo Verde",
|
||||
"Ciad",
|
||||
"Cile",
|
||||
"Cina",
|
||||
"Cipro",
|
||||
"Città del Vaticano",
|
||||
"Colombia",
|
||||
"Comore",
|
||||
"Corea del Nord",
|
||||
"Corea del Sud",
|
||||
"Costa Rica",
|
||||
"Costa d'Avorio",
|
||||
"Croazia",
|
||||
"Cuba",
|
||||
"Danimarca",
|
||||
"Dominica",
|
||||
"Ecuador",
|
||||
"Egitto",
|
||||
"El Salvador",
|
||||
"Emirati Arabi Uniti",
|
||||
"Eritrea",
|
||||
"Estonia",
|
||||
"Etiopia",
|
||||
"Fiji",
|
||||
"Filippine",
|
||||
"Finlandia",
|
||||
"Francia",
|
||||
"Gabon",
|
||||
"Gambia",
|
||||
"Georgia",
|
||||
"Georgia del Sud e isole Sandwich",
|
||||
"Germania",
|
||||
"Ghana",
|
||||
"Giamaica",
|
||||
"Giappone",
|
||||
"Gibilterra",
|
||||
"Gibuti",
|
||||
"Giordania",
|
||||
"Grecia",
|
||||
"Grenada",
|
||||
"Groenlandia",
|
||||
"Guadalupa",
|
||||
"Guam",
|
||||
"Guatemala",
|
||||
"Guernsey - Channel Islands",
|
||||
"Guinea",
|
||||
"Guinea Equatoriale",
|
||||
"Guinea-Bissau",
|
||||
"Guyana",
|
||||
"Guyana Francese",
|
||||
"Haiti",
|
||||
"Heard Island e McDonald Islands",
|
||||
"Honduras",
|
||||
"Hong Kong",
|
||||
"India",
|
||||
"Indonesia",
|
||||
"Iran",
|
||||
"Iraq",
|
||||
"Irlanda",
|
||||
"Islanda",
|
||||
"Isola Bouvet",
|
||||
"Isola Christmas",
|
||||
"Isola di Man",
|
||||
"Isola Norfolk",
|
||||
"Isole Canarie (Spagna)",
|
||||
"Isole Cayman",
|
||||
"Isole Cocos e Keeling",
|
||||
"Isole Cook",
|
||||
"Isole Falkland",
|
||||
"Isole Fær Øer",
|
||||
"Isole Marianne Settentrionali",
|
||||
"Isole Marshall",
|
||||
"Isole Minori Esterne degli USA",
|
||||
"Isole Pitcairn",
|
||||
"Isole Salomone",
|
||||
"Isole Vergini Americane",
|
||||
"Isole Vergini Britanniche",
|
||||
"Isole Åland",
|
||||
"Israele",
|
||||
"Jersey - Channel Islands",
|
||||
"Kazakistan",
|
||||
"Kenia",
|
||||
"Kirghizistan",
|
||||
"Kiribati",
|
||||
"Kosovo",
|
||||
"Kuwait",
|
||||
"Laos",
|
||||
"Lesotho",
|
||||
"Lettonia",
|
||||
"Libano",
|
||||
"Liberia",
|
||||
"Libia",
|
||||
"Liechtenstein",
|
||||
"Lituania",
|
||||
"Lussemburgo",
|
||||
"Macao",
|
||||
"Macedonia",
|
||||
"Macedonia del Nord",
|
||||
"Madagascar",
|
||||
"Malawi",
|
||||
"Malaysia",
|
||||
"Maldive",
|
||||
"Mali",
|
||||
"Malta",
|
||||
"Marocco",
|
||||
"Martinica",
|
||||
"Mauritania",
|
||||
"Mauritius",
|
||||
"Mayotte",
|
||||
"Messico",
|
||||
"Micronesia",
|
||||
"Moldavia",
|
||||
"Monaco",
|
||||
"Mongolia",
|
||||
"Montenegro",
|
||||
"Montserrat",
|
||||
"Mozambico",
|
||||
"Myanmar",
|
||||
"Namibia",
|
||||
"Nauru",
|
||||
"Nepal",
|
||||
"Nicaragua",
|
||||
"Niger",
|
||||
"Nigeria",
|
||||
"Niue",
|
||||
"Norvegia",
|
||||
"Nuova Caledonia",
|
||||
"Nuova Zelanda",
|
||||
"Oman",
|
||||
"Paesi Bassi",
|
||||
"Pakistan",
|
||||
"Palau",
|
||||
"Panama",
|
||||
"Papua Nuova Guinea",
|
||||
"Paraguay",
|
||||
"Perù",
|
||||
"Polinesia Francese",
|
||||
"Polonia",
|
||||
"Porto Rico",
|
||||
"Portogallo",
|
||||
"Qatar",
|
||||
"Regno Unito",
|
||||
"Repubblica Ceca",
|
||||
"Repubblica Centrafricana",
|
||||
"Repubblica Democratica del Congo",
|
||||
"Repubblica Dominicana",
|
||||
"Repubblica del Congo",
|
||||
"Riunione",
|
||||
"Romania",
|
||||
"Ruanda",
|
||||
"Russia",
|
||||
"Sahara Occidentale",
|
||||
"Saint Barthelemy",
|
||||
"Saint Kitts e Nevis",
|
||||
"Saint Martin",
|
||||
"Saint Pierre e Miquelon",
|
||||
"Saint Vincent e Grenadine",
|
||||
"Samoa",
|
||||
"Samoa Americane",
|
||||
"San Marino",
|
||||
"Sant'Elena",
|
||||
"Santa Lucia",
|
||||
"Senegal",
|
||||
"Serbia",
|
||||
"Seychelles",
|
||||
"Sierra Leone",
|
||||
"Singapore",
|
||||
"Siria",
|
||||
"Slovacchia",
|
||||
"Slovenia",
|
||||
"Somalia",
|
||||
"Spagna",
|
||||
"Sri Lanka",
|
||||
"Stato di Palestina",
|
||||
"Stati Uniti",
|
||||
"Sudafrica",
|
||||
"Sud Sudan",
|
||||
"Sudan",
|
||||
"Suriname",
|
||||
"Svalbard e Jan Mayen",
|
||||
"Svezia",
|
||||
"Svizzera",
|
||||
"Swaziland",
|
||||
"São Tomé e Príncipe",
|
||||
"Tagikistan",
|
||||
"Taiwan",
|
||||
"Tanzania",
|
||||
"Terre australi e ant. francesi",
|
||||
"Terr. Britannico dell'Oc. Ind.",
|
||||
"Thailandia",
|
||||
"Timor Est",
|
||||
"Togo",
|
||||
"Tokelau",
|
||||
"Tonga",
|
||||
"Trinidad e Tobago",
|
||||
"Tunisia",
|
||||
"Turchia",
|
||||
"Turkmenistan",
|
||||
"Turks e Caicos",
|
||||
"Tuvalu",
|
||||
"Ucraina",
|
||||
"Uganda",
|
||||
"Ungheria",
|
||||
"Uruguay",
|
||||
"Uzbekistan",
|
||||
"Vanuatu",
|
||||
"Venezuela",
|
||||
"Vietnam",
|
||||
"Wallis e Futuna",
|
||||
"Yemen",
|
||||
"Zambia",
|
||||
"Zimbabwe",
|
||||
"Irlanda del Nord",
|
||||
];*/
|
||||
|
|
@ -1,7 +1,11 @@
|
|||
import { z } from "zod/v4";
|
||||
|
||||
import { adminProcedure, createTRPCRouter } from "~/server/api/trpc";
|
||||
import { getClientFromCF } from "~/server/controllers/fic.controller";
|
||||
import {
|
||||
exportUserToFIC,
|
||||
getClientFromCF,
|
||||
} from "~/server/controllers/fic.controller";
|
||||
import { zUserId } from "~/server/utils/zod_types";
|
||||
|
||||
export const fattureRouter = createTRPCRouter({
|
||||
getClientFromCF: adminProcedure
|
||||
|
|
@ -13,4 +17,13 @@ export const fattureRouter = createTRPCRouter({
|
|||
.query(async ({ input }) => {
|
||||
return await getClientFromCF(input.cf);
|
||||
}),
|
||||
addToFIC: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
userId: zUserId,
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input }) => {
|
||||
return await exportUserToFIC(input.userId);
|
||||
}),
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,9 +1,14 @@
|
|||
import { TRPCError } from "@trpc/server";
|
||||
import { sql } from "kysely";
|
||||
import { db } from "../db";
|
||||
|
||||
export const getComuni = async () => {
|
||||
try {
|
||||
return await db.selectFrom("comuni").selectAll().execute();
|
||||
return await db
|
||||
.selectFrom("comuni")
|
||||
.selectAll()
|
||||
.orderBy("nome", "asc")
|
||||
.execute();
|
||||
} catch (e) {
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
|
|
@ -17,6 +22,7 @@ export const searchComuniByCatasto = async (catasto: string[]) => {
|
|||
return await db
|
||||
.selectFrom("comuni")
|
||||
.selectAll()
|
||||
.orderBy("nome", "asc")
|
||||
.where("catasto", "in", catasto)
|
||||
.execute();
|
||||
} catch (e) {
|
||||
|
|
@ -40,7 +46,12 @@ export const getProvincie = async () => {
|
|||
|
||||
export const getNazioni = async () => {
|
||||
try {
|
||||
return await db.selectFrom("nazioni").selectAll().execute();
|
||||
return await db
|
||||
.selectFrom("nazioni")
|
||||
.selectAll()
|
||||
.orderBy(sql`nome = 'ITALIA'`, "desc")
|
||||
.orderBy("nome", "asc")
|
||||
.execute();
|
||||
} catch (e) {
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
|
|
|
|||
|
|
@ -1,3 +1,7 @@
|
|||
import type { Client } from "@fattureincloud/fattureincloud-ts-sdk";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import type { UsersId } from "~/schemas/public/Users";
|
||||
import { db } from "~/server/db";
|
||||
import { clientsApi, companyId } from "~/server/FattureInCloud";
|
||||
|
||||
// export const getClientList = async () => {
|
||||
|
|
@ -40,7 +44,216 @@ export const getClientFromCF = async (cf: string) => {
|
|||
return clients.data;
|
||||
};
|
||||
|
||||
// export const addClient = async (client: Client) => {
|
||||
// const newClient = await clientsApi.createClient(companyId, { data: client });
|
||||
// return newClient.data;
|
||||
export const addClient = async (client: Client) => {
|
||||
const newClient = await clientsApi.createClient(companyId, { data: client });
|
||||
return newClient.data;
|
||||
};
|
||||
|
||||
export const exportUserToFIC = async (userId: UsersId) => {
|
||||
try {
|
||||
const user = await db
|
||||
.selectFrom("users")
|
||||
.innerJoin("users_anagrafica", "users_anagrafica.userid", "users.id")
|
||||
.select([
|
||||
"users.email",
|
||||
"users.nome",
|
||||
"users.cognome",
|
||||
"users.telefono",
|
||||
"users_anagrafica.codice_fiscale",
|
||||
"users_anagrafica.provincia_residenza",
|
||||
"users_anagrafica.comune_residenza",
|
||||
"users_anagrafica.cap_residenza",
|
||||
"users_anagrafica.via_residenza",
|
||||
"users_anagrafica.civico_residenza",
|
||||
])
|
||||
.leftJoin(
|
||||
"nazioni",
|
||||
"nazioni.catasto",
|
||||
"users_anagrafica.nazione_residenza",
|
||||
)
|
||||
.select("nazioni.iso as nazione_iso")
|
||||
.leftJoin("comuni", "comuni.catasto", "users_anagrafica.comune_residenza")
|
||||
.select("comuni.nome as comune_nome")
|
||||
.where("users.id", "=", userId)
|
||||
.executeTakeFirstOrThrow();
|
||||
if (!user.nazione_iso) {
|
||||
throw new Error("Nazione non compatibile con esportazione FIC");
|
||||
}
|
||||
|
||||
if (
|
||||
!user.cap_residenza ||
|
||||
!user.comune_residenza ||
|
||||
!user.provincia_residenza ||
|
||||
!user.via_residenza ||
|
||||
!user.nazione_iso
|
||||
) {
|
||||
throw new Error("Utente mancante dati necessari per esportazione FIC");
|
||||
}
|
||||
|
||||
const comune =
|
||||
user.nazione_iso === "IT"
|
||||
? user.comune_nome || user.comune_residenza
|
||||
: user.comune_residenza;
|
||||
const clientData: Client = {
|
||||
name: `${user.cognome.toUpperCase()} ${user.nome.toUpperCase()}`.trim(),
|
||||
type: "person",
|
||||
first_name: user.nome.toUpperCase(),
|
||||
last_name: user.cognome.toUpperCase(),
|
||||
tax_code: user.codice_fiscale,
|
||||
address_street: `${user.via_residenza.toUpperCase().replace(".", "")}${user.civico_residenza ? ` ${user.civico_residenza}` : ""}`,
|
||||
address_postal_code: user.cap_residenza,
|
||||
address_city: comune.toUpperCase(),
|
||||
address_province: user.provincia_residenza.toUpperCase(),
|
||||
country_iso: user.nazione_iso,
|
||||
email: user.email,
|
||||
phone: user.telefono || undefined,
|
||||
ei_code: "0000000",
|
||||
};
|
||||
|
||||
const req = await clientsApi.createClient(companyId, { data: clientData });
|
||||
|
||||
if (req.status !== 200) {
|
||||
throw new Error(`Failed to create client in FIC: ${req.statusText}`);
|
||||
}
|
||||
|
||||
return req.data;
|
||||
} catch (error) {
|
||||
if (error instanceof Error && "response" in error) {
|
||||
// Log the actual FIC validation response
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <ok>
|
||||
console.error("FIC API response data:", (error as any).response?.data);
|
||||
}
|
||||
console.error("Error exporting user to FIC:", error);
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "Failed to export user to Fatture In Cloud",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// function normalizeString(str: string): string {
|
||||
// return str
|
||||
// .toUpperCase()
|
||||
// .replace("E'", "E")
|
||||
// .replace("A'", "A")
|
||||
// .replace("U'", "U")
|
||||
// .replace("I'", "I")
|
||||
// .replace("O'", "O")
|
||||
// .replace("È", "E")
|
||||
// .replace("É", "E")
|
||||
// .replace("À", "A")
|
||||
// .replace("Ù", "U")
|
||||
// .replace("Ì", "I")
|
||||
// .replace("Ò", "O");
|
||||
// }
|
||||
|
||||
// type FICNation = {
|
||||
// name: string;
|
||||
// settings_name: string;
|
||||
// iso: string;
|
||||
// fiscal_iso: string;
|
||||
// uic: string;
|
||||
// banned: boolean;
|
||||
// };
|
||||
|
||||
// const KnownConflicts: Record<string, string> = {
|
||||
// BAHRAIN: "Z204",
|
||||
// "BOSNIA ED ERZEGOVINA": "Z153",
|
||||
// CINA: "Z210",
|
||||
// KAZAKISTAN: "Z255",
|
||||
// KENIA: "Z322",
|
||||
// MACEDONIA: "Z148",
|
||||
// MOLDAVIA: "Z140",
|
||||
// "PORTO RICO": "Z518",
|
||||
// RUSSIA: "Z154",
|
||||
// "STATI UNITI": "Z404",
|
||||
// };
|
||||
|
||||
// export const nazioniSync = async () => {
|
||||
// const raw = readFileSync("fic_nations.json", "utf-8");
|
||||
// const data: FICNation[] = (JSON.parse(raw) as { data: FICNation[] }).data;
|
||||
// console.log(`Parsed ${data.length} FIC nations`);
|
||||
// // console.log(data);
|
||||
// const dbData = await db.selectFrom("nazioni").selectAll().execute();
|
||||
// const dbNazioniMap = new Map(
|
||||
// dbData.map((nazione) => [normalizeString(nazione.nome), nazione]),
|
||||
// );
|
||||
// const toUpdate: { catasto: string; iso: string }[] = [];
|
||||
// for (const nation of data) {
|
||||
// const normalizedName = normalizeString(nation.name);
|
||||
// const existing = dbNazioniMap.get(normalizedName);
|
||||
// if (existing) {
|
||||
// if (existing.iso !== nation.iso) {
|
||||
// // await db
|
||||
// // .updateTable("nazioni")
|
||||
// // .set({ iso: nation.iso })
|
||||
// // .where("id", "=", existing.id)
|
||||
// // .execute();
|
||||
// toUpdate.push({ catasto: existing.catasto, iso: nation.iso });
|
||||
// }
|
||||
// } else {
|
||||
// const k = KnownConflicts[normalizedName];
|
||||
// if (k) {
|
||||
// toUpdate.push({ catasto: k, iso: nation.iso });
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// console.warn(
|
||||
// `Nation ${nation.name.toUpperCase()} not found in DB, consider adding it with ISO ${nation.iso}`,
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
// console.log("Nazioni to update:", toUpdate);
|
||||
// //write a txt file with the update statements
|
||||
// const updateStatements = toUpdate
|
||||
// .map((u) => `('${u.catasto}', '${u.iso}')`)
|
||||
// .join(",\n");
|
||||
// const sql = `UPDATE nazioni SET iso = data.iso FROM (VALUES\n${updateStatements}\n) AS data(catasto, iso) WHERE nazioni.catasto = data.catasto;`;
|
||||
|
||||
// writeFileSync("nazioni_update.sql", sql);
|
||||
//};
|
||||
|
||||
// export const ficCapSync = async () => {
|
||||
// try {
|
||||
// const comuni = await infosApi.listCities();
|
||||
// const nazioni = await infosApi.listDetailedCountries();
|
||||
// if (comuni.status === 200) {
|
||||
// const data = comuni.data.data;
|
||||
// if (data) {
|
||||
// await db.transaction().execute(async (trx) => {
|
||||
// for (const comune of data) {
|
||||
// if (comune.city && comune.postal_code && comune.province) {
|
||||
// await trx
|
||||
// .updateTable("comuni")
|
||||
// .set({
|
||||
// cap: comune.postal_code,
|
||||
// })
|
||||
// .where("nome", "=", comune.city.toUpperCase())
|
||||
// .execute();
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// if (nazioni.status === 200) {
|
||||
// const data = nazioni.data.data;
|
||||
// if (data) {
|
||||
// await db.transaction().execute(async (trx) => {
|
||||
// for (const nazione of data) {
|
||||
// if (nazione.name && nazione.iso) {
|
||||
// await trx
|
||||
// .updateTable("nazioni")
|
||||
// .set({
|
||||
// iso: nazione.iso,
|
||||
// })
|
||||
// .where("nazioni.nome", "=", nazione.name.toUpperCase())
|
||||
// .execute();
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// } catch (error) {
|
||||
// console.error("Error syncing FIC catasto:", error);
|
||||
// }
|
||||
// };
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue