Merge branch 'main' into B2B-88
This commit is contained in:
10
lib/actions/sign-out.tsx
Normal file
10
lib/actions/sign-out.tsx
Normal file
@@ -0,0 +1,10 @@
|
||||
"use server";
|
||||
|
||||
import { createClient } from "@/utils/supabase/server";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
export const signOutAction = async () => {
|
||||
const supabase = await createClient();
|
||||
await supabase.auth.signOut();
|
||||
return redirect("/sign-in");
|
||||
};
|
||||
27
lib/fonts.ts
27
lib/fonts.ts
@@ -1,4 +1,5 @@
|
||||
import { Inter as SansFont } from 'next/font/google';
|
||||
import { Geist as HeadingFont } from 'next/font/google';
|
||||
import SansFont from 'next/font/local';
|
||||
|
||||
import { cn } from '@kit/ui/utils';
|
||||
|
||||
@@ -8,18 +9,32 @@ import { cn } from '@kit/ui/utils';
|
||||
* By default, it uses the Inter font from Google Fonts.
|
||||
*/
|
||||
const sans = SansFont({
|
||||
subsets: ['latin'],
|
||||
variable: '--font-sans',
|
||||
fallback: ['system-ui', 'Helvetica Neue', 'Helvetica', 'Arial'],
|
||||
preload: true,
|
||||
weight: ['300', '400', '500', '600', '700'],
|
||||
src: [
|
||||
{
|
||||
path: '../fonts/InterDisplay-Regular.woff2',
|
||||
weight: '400',
|
||||
style: 'normal',
|
||||
},
|
||||
{
|
||||
path: '../fonts/InterDisplay-Medium.woff2',
|
||||
weight: '500',
|
||||
style: 'medium',
|
||||
},
|
||||
],
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* @heading
|
||||
* @description Define here the heading font.
|
||||
*/
|
||||
const heading = sans;
|
||||
const heading = HeadingFont({
|
||||
variable: '--font-heading',
|
||||
fallback: ['system-ui', 'Helvetica Neue', 'Helvetica', 'Arial'],
|
||||
preload: true,
|
||||
weight: ['300', '400', '500', '600', '700'],
|
||||
});
|
||||
|
||||
// we export these fonts into the root layout
|
||||
export { sans, heading };
|
||||
|
||||
@@ -12,7 +12,7 @@ const defaultLanguage = process.env.NEXT_PUBLIC_DEFAULT_LOCALE ?? 'en';
|
||||
* By default, only the default language is supported.
|
||||
* Add more languages here if needed.
|
||||
*/
|
||||
export const languages: string[] = [defaultLanguage];
|
||||
export const languages: string[] = [defaultLanguage, 'en', 'ru'];
|
||||
|
||||
/**
|
||||
* The name of the cookie that stores the selected language.
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
import {
|
||||
GetMessageListResponse,
|
||||
MedipostAction,
|
||||
MedipostPublicMessageResponse,
|
||||
Message,
|
||||
UuringuGrupp,
|
||||
} from "@/lib/types/medipost";
|
||||
import { Tables } from "@/supabase/database.types";
|
||||
import { createClient, SupabaseClient } from "@supabase/supabase-js";
|
||||
import axios from "axios";
|
||||
import { xml2json } from "xml-js";
|
||||
import { XMLParser } from "fast-xml-parser";
|
||||
import { SyncStatus } from "@/lib/types/audit";
|
||||
import { toArray } from "@/lib/utils";
|
||||
|
||||
const BASE_URL = process.env.MEDIPOST_URL!;
|
||||
const USER = process.env.MEDIPOST_USER!;
|
||||
@@ -15,9 +21,10 @@ export async function getMessages() {
|
||||
const publicMessage = await getLatestPublicMessageListItem();
|
||||
|
||||
if (!publicMessage) {
|
||||
return [];
|
||||
return null;
|
||||
}
|
||||
|
||||
//Teenused tuleb mappida kokku MedReport teenustega. <UuringId> alusel
|
||||
return getPublicMessage(publicMessage.messageId);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
@@ -55,18 +62,13 @@ export async function getPublicMessage(messageId: string) {
|
||||
Accept: "application/xml",
|
||||
},
|
||||
});
|
||||
const parser = new XMLParser({ ignoreAttributes: false });
|
||||
const parsed: MedipostPublicMessageResponse = parser.parse(data);
|
||||
|
||||
if (data.code && data.code !== 0) {
|
||||
if (parsed.ANSWER?.CODE && parsed.ANSWER?.CODE !== 0) {
|
||||
throw new Error(`Failed to get public message (id: ${messageId})`);
|
||||
}
|
||||
|
||||
const parsed = JSON.parse(
|
||||
xml2json(data, {
|
||||
compact: true,
|
||||
spaces: 2,
|
||||
})
|
||||
);
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
@@ -124,14 +126,8 @@ export async function getPrivateMessage(messageId: string) {
|
||||
throw new Error(`Failed to get private message (id: ${messageId})`);
|
||||
}
|
||||
|
||||
const parsed = JSON.parse(
|
||||
xml2json(data, {
|
||||
compact: true,
|
||||
spaces: 2,
|
||||
})
|
||||
);
|
||||
|
||||
return parsed;
|
||||
const parser = new XMLParser({ ignoreAttributes: false });
|
||||
return parser.parse(data);
|
||||
}
|
||||
|
||||
export async function deletePrivateMessage(messageId: string) {
|
||||
@@ -170,6 +166,187 @@ export async function readPrivateMessageResponse() {
|
||||
}
|
||||
}
|
||||
|
||||
async function saveAnalysisGroup(
|
||||
analysisGroup: UuringuGrupp,
|
||||
supabase: SupabaseClient
|
||||
) {
|
||||
const { data: insertedAnalysisGroup, error } = await supabase
|
||||
.from("analysis_groups")
|
||||
.upsert(
|
||||
{
|
||||
original_id: analysisGroup.UuringuGruppId,
|
||||
name: analysisGroup.UuringuGruppNimi,
|
||||
order: analysisGroup.UuringuGruppJarjekord,
|
||||
},
|
||||
{ onConflict: "original_id", ignoreDuplicates: false }
|
||||
)
|
||||
.select("id");
|
||||
|
||||
if (error || !insertedAnalysisGroup[0]?.id) {
|
||||
throw new Error(
|
||||
`Failed to insert analysis group (id: ${analysisGroup.UuringuGruppId}), error: ${error?.message}`
|
||||
);
|
||||
}
|
||||
const analysisGroupId = insertedAnalysisGroup[0].id;
|
||||
|
||||
const analysisGroupCodes = toArray(analysisGroup.Kood);
|
||||
const codes: Partial<Tables<"codes">>[] = analysisGroupCodes.map((kood) => ({
|
||||
hk_code: kood.HkKood,
|
||||
hk_code_multiplier: kood.HkKoodiKordaja,
|
||||
coefficient: kood.Koefitsient,
|
||||
price: kood.Hind,
|
||||
analysis_group_id: analysisGroupId,
|
||||
}));
|
||||
|
||||
const analysisGroupItems = toArray(analysisGroup.Uuring);
|
||||
|
||||
for (const item of analysisGroupItems) {
|
||||
const analysisElement = item.UuringuElement;
|
||||
|
||||
const { data: insertedAnalysisElement, error } = await supabase
|
||||
.from("analysis_elements")
|
||||
.upsert(
|
||||
{
|
||||
analysis_id_oid: analysisElement.UuringIdOID,
|
||||
analysis_id_original: analysisElement.UuringId,
|
||||
tehik_short_loinc: analysisElement.TLyhend,
|
||||
tehik_loinc_name: analysisElement.KNimetus,
|
||||
analysis_name_lab: analysisElement.UuringNimi,
|
||||
order: analysisElement.Jarjekord,
|
||||
parent_analysis_group_id: analysisGroupId,
|
||||
material_groups: toArray(item.MaterjalideGrupp),
|
||||
},
|
||||
{ onConflict: "analysis_id_original", ignoreDuplicates: false }
|
||||
)
|
||||
.select('id');
|
||||
|
||||
if (error || !insertedAnalysisElement[0]?.id) {
|
||||
throw new Error(
|
||||
`Failed to insert analysis element (id: ${analysisElement.UuringId}), error: ${error?.message}`
|
||||
);
|
||||
}
|
||||
|
||||
const insertedAnalysisElementId = insertedAnalysisElement[0].id;
|
||||
|
||||
if (analysisElement.Kood) {
|
||||
const analysisElementCodes = toArray(analysisElement.Kood);
|
||||
codes.push(
|
||||
...analysisElementCodes.map((kood) => ({
|
||||
hk_code: kood.HkKood,
|
||||
hk_code_multiplier: kood.HkKoodiKordaja,
|
||||
coefficient: kood.Koefitsient,
|
||||
price: kood.Hind,
|
||||
analysis_element_id: insertedAnalysisElementId,
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
const analyses = analysisElement.UuringuElement;
|
||||
if (analyses?.length) {
|
||||
for (const analysis of analyses) {
|
||||
const { data: insertedAnalysis, error } = await supabase
|
||||
.from("analyses")
|
||||
.upsert(
|
||||
{
|
||||
analysis_id_oid: analysis.UuringIdOID,
|
||||
analysis_id_original: analysis.UuringId,
|
||||
tehik_short_loinc: analysis.TLyhend,
|
||||
tehik_loinc_name: analysis.KNimetus,
|
||||
analysis_name_lab: analysis.UuringNimi,
|
||||
order: analysis.Jarjekord,
|
||||
parent_analysis_element_id: insertedAnalysisElementId,
|
||||
},
|
||||
{ onConflict: "analysis_id_original", ignoreDuplicates: false }
|
||||
)
|
||||
.select('id');
|
||||
|
||||
if (error || !insertedAnalysis[0]?.id) {
|
||||
throw new Error(
|
||||
`Failed to insert analysis (id: ${analysis.UuringId}) error: ${error?.message}`
|
||||
);
|
||||
}
|
||||
|
||||
const insertedAnalysisId = insertedAnalysis[0].id;
|
||||
if (analysisElement.Kood) {
|
||||
const analysisCodes = toArray(analysis.Kood);
|
||||
|
||||
codes.push(
|
||||
...analysisCodes.map((kood) => ({
|
||||
hk_code: kood.HkKood,
|
||||
hk_code_multiplier: kood.HkKoodiKordaja,
|
||||
coefficient: kood.Koefitsient,
|
||||
price: kood.Hind,
|
||||
analysis_id: insertedAnalysisId,
|
||||
}))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const { error: codesError } = await supabase
|
||||
.from("codes")
|
||||
.upsert(codes, { ignoreDuplicates: false });
|
||||
|
||||
if (codesError?.code) {
|
||||
throw new Error(
|
||||
`Failed to insert codes (analysis group id: ${analysisGroup.UuringuGruppId})`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function syncPublicMessage(
|
||||
message?: MedipostPublicMessageResponse | null
|
||||
) {
|
||||
const supabase = createClient(
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||
process.env.NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY!,
|
||||
{
|
||||
auth: {
|
||||
persistSession: false,
|
||||
autoRefreshToken: false,
|
||||
detectSessionInUrl: false,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
try {
|
||||
const providers = toArray(message?.Saadetis?.Teenused.Teostaja);
|
||||
const analysisGroups = providers.flatMap((provider) =>
|
||||
toArray(provider.UuringuGrupp)
|
||||
);
|
||||
if (!message || !analysisGroups.length) {
|
||||
return supabase.schema("audit").from("sync_entries").insert({
|
||||
operation: "ANALYSES_SYNC",
|
||||
comment: "No data received",
|
||||
status: SyncStatus.Fail,
|
||||
changed_by_role: "service_role",
|
||||
});
|
||||
}
|
||||
|
||||
for (const analysisGroup of analysisGroups) {
|
||||
await saveAnalysisGroup(analysisGroup, supabase);
|
||||
}
|
||||
|
||||
await supabase.schema("audit").from("sync_entries").insert({
|
||||
operation: "ANALYSES_SYNC",
|
||||
status: SyncStatus.Success,
|
||||
changed_by_role: "service_role",
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
await supabase
|
||||
.schema("audit")
|
||||
.from("sync_entries")
|
||||
.insert({
|
||||
operation: "ANALYSES_SYNC",
|
||||
status: SyncStatus.Fail,
|
||||
comment: JSON.stringify(e),
|
||||
changed_by_role: "service_role",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function getLatestMessage(messages?: Message[]) {
|
||||
if (!messages?.length) {
|
||||
return null;
|
||||
|
||||
31
lib/services/register-company.service.ts
Normal file
31
lib/services/register-company.service.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
"use server";
|
||||
|
||||
import * as yup from "yup";
|
||||
import { companySchema } from "@/lib/validations/companySchema";
|
||||
|
||||
export async function submitCompanyRegistration(formData: FormData) {
|
||||
const data = {
|
||||
companyName: formData.get("companyName")?.toString() || "",
|
||||
contactPerson: formData.get("contactPerson")?.toString() || "",
|
||||
email: formData.get("email")?.toString() || "",
|
||||
phone: formData.get("phone")?.toString() || "",
|
||||
};
|
||||
|
||||
try {
|
||||
await companySchema.validate(data, { abortEarly: false });
|
||||
|
||||
console.log("Valid data:", data);
|
||||
} catch (validationError) {
|
||||
if (validationError instanceof yup.ValidationError) {
|
||||
const errors = validationError.inner.map((err) => ({
|
||||
path: err.path,
|
||||
message: err.message,
|
||||
}));
|
||||
throw new Error(
|
||||
"Validation failed: " +
|
||||
errors.map((e) => `${e.path}: ${e.message}`).join(", ")
|
||||
);
|
||||
}
|
||||
throw validationError;
|
||||
}
|
||||
}
|
||||
4
lib/types/audit.ts
Normal file
4
lib/types/audit.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export enum SyncStatus {
|
||||
Success = "SUCCESS",
|
||||
Fail = "FAIL",
|
||||
}
|
||||
6
lib/types/company.ts
Normal file
6
lib/types/company.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export interface CompanySubmitData {
|
||||
companyName: string;
|
||||
contactPerson: string;
|
||||
email: string;
|
||||
phone?: string;
|
||||
}
|
||||
@@ -20,3 +20,122 @@ export enum MedipostAction {
|
||||
GetPrivateMessage = "GetPrivateMessage",
|
||||
DeletePrivateMessage = "DeletePrivateMessage",
|
||||
}
|
||||
|
||||
export type VoimalikVaartus = {
|
||||
VaartusId: string;
|
||||
Vaartus: "jah" | "ei";
|
||||
VaartusJarjekord: number;
|
||||
};
|
||||
|
||||
export type Sisendparameeter = {
|
||||
"@_VastuseTyyp"?: "ARV" | "VABATEKST" | "KODEERITUD" | "AJAHETK";
|
||||
"@_VastuseKoodistikuOID"?: string;
|
||||
"@_VastuseKoodistikuNimi"?: string;
|
||||
"@_URL"?: string;
|
||||
|
||||
UuringIdOID: string;
|
||||
UuringId: string;
|
||||
TLyhend: string;
|
||||
KNimetus: string;
|
||||
UuringNimi: string;
|
||||
Jarjekord: number;
|
||||
|
||||
VoimalikVaartus: VoimalikVaartus[];
|
||||
};
|
||||
|
||||
export type Kood = {
|
||||
HkKood: string;
|
||||
HkKoodiKordaja: number;
|
||||
Koefitsient: number; // float
|
||||
Hind: number; // float
|
||||
};
|
||||
|
||||
export type UuringuAlamElement = {
|
||||
UuringIdOID: string;
|
||||
UuringId: string;
|
||||
TLyhend: string;
|
||||
KNimetus: string;
|
||||
UuringNimi: string;
|
||||
Jarjekord: string;
|
||||
Kood?: Kood[];
|
||||
};
|
||||
|
||||
export type UuringuElement = {
|
||||
UuringIdOID: string;
|
||||
UuringId: string;
|
||||
TLyhend: string;
|
||||
KNimetus: string;
|
||||
UuringNimi: string;
|
||||
Jarjekord: string;
|
||||
Kood?: Kood[];
|
||||
UuringuElement?: UuringuAlamElement[];
|
||||
};
|
||||
|
||||
export type Uuring = {
|
||||
tellitav: "JAH" | "EI";
|
||||
UuringuElement: UuringuElement; //1..1
|
||||
MaterjalideGrupp?: MaterjalideGrupp[]; //0..n
|
||||
};
|
||||
|
||||
export type UuringuGrupp = {
|
||||
UuringuGruppId: string;
|
||||
UuringuGruppNimi: string;
|
||||
UuringuGruppJarjekord: number;
|
||||
Uuring: Uuring | Uuring[]; //1..n
|
||||
Kood?: Kood | Kood[]; //0..n
|
||||
};
|
||||
|
||||
export type Konteiner = {
|
||||
ProovinouKoodOID: string;
|
||||
ProovinouKood: string;
|
||||
KonteineriNimi: string;
|
||||
KonteineriKirjeldus: string;
|
||||
};
|
||||
|
||||
export type Materjal = {
|
||||
MaterjaliTyypOID: string;
|
||||
MaterjaliTyyp: string;
|
||||
MaterjaliNimi: string;
|
||||
KonteineriOmadus: string;
|
||||
MaterjaliPaige: { Kohustuslik: "JAH" | "EI" }; //0..1
|
||||
Konteiner?: Konteiner[]; //0..n
|
||||
};
|
||||
|
||||
export type MaterjalideGrupp = {
|
||||
vaikimisi: "JAH" | "EI";
|
||||
Materjal: Materjal; //1..n
|
||||
};
|
||||
|
||||
export type Teostaja = {
|
||||
UuringuGrupp?: UuringuGrupp | UuringuGrupp[]; //0...n
|
||||
Asutus: {
|
||||
AsutuseId: string;
|
||||
AsutuseNimi: string;
|
||||
AsutuseKood: string;
|
||||
AllyksuseNimi: string;
|
||||
Telefon: string;
|
||||
Aadress: string;
|
||||
};
|
||||
Sisendparameeter?: Sisendparameeter | Sisendparameeter[]; //0...n
|
||||
};
|
||||
|
||||
export type MedipostPublicMessageResponse = {
|
||||
"?xml": {
|
||||
"@_version": string;
|
||||
"@_encoding": "UTF-8";
|
||||
"@_standalone"?: "yes" | "no";
|
||||
};
|
||||
ANSWER?: { CODE: number };
|
||||
Saadetis?: {
|
||||
Pais: {
|
||||
Pakett: { "#text": "SL" | "OL" | "AL" | "ME" }; // SL - Teenused, OL - Tellimus (meie poolt saadetav saatekiri), AL - Vastus (saatekirja vastus), ME - Teade
|
||||
Saatja: string;
|
||||
Saaja: string;
|
||||
Aeg: string;
|
||||
SaadetisId: string;
|
||||
};
|
||||
Teenused: {
|
||||
Teostaja: Teostaja | Teostaja[]; //1..n
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
@@ -4,3 +4,8 @@ import { twMerge } from "tailwind-merge";
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
|
||||
export function toArray<T>(input?: T | T[] | null): T[] {
|
||||
if (!input) return [];
|
||||
return Array.isArray(input) ? input : [input];
|
||||
}
|
||||
|
||||
8
lib/validations/companySchema.ts
Normal file
8
lib/validations/companySchema.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import * as yup from "yup";
|
||||
|
||||
export const companySchema = yup.object({
|
||||
companyName: yup.string().required("Company name is required"),
|
||||
contactPerson: yup.string().required("Contact person is required"),
|
||||
email: yup.string().email("Invalid email").required("Email is required"),
|
||||
phone: yup.string().optional(),
|
||||
});
|
||||
Reference in New Issue
Block a user