import { IpcMainEvent, shell } from "electron"; import log from "electron-log/main"; import { autoUpdater } from "electron-updater"; import { User } from "firebase/auth"; import client from "../graphql/graphql-client"; import { ActiveBodyshopQueryResult, MasterdataQueryResult, QUERY_ACTIVE_BODYSHOP_TYPED, QUERY_MASTERDATA_TYPED, } from "../graphql/queries"; import Store from "../store/store"; import errorTypeCheck from "../../util/errorTypeCheck"; import { sendIpcToRenderer } from "../util/toRenderer"; import ipcTypes from "../../util/ipcTypes.json"; const ipcMainHandleAuthStateChanged = async ( _event: IpcMainEvent, user: User | null, ): Promise => { Store.set("user", user); try { //Need to query the currently active shop, and store the metadata as well. //Also need to query the OP Codes for decoding reference. const activeBodyshop: ActiveBodyshopQueryResult = await client.request( QUERY_ACTIVE_BODYSHOP_TYPED, ); Store.set("app.bodyshop", activeBodyshop.bodyshops[0]); const OpCodes: MasterdataQueryResult = await client.request( QUERY_MASTERDATA_TYPED, { key: `${activeBodyshop.bodyshops[0].region_config}_ciecaopcodes`, }, ); Store.set( "app.masterdata.opcodes", JSON.parse(OpCodes.masterdata[0]?.value), ); log.debug("Received authentication state change from Renderer.", user); log.debug("Requery shop information & master data."); //Check for updates const convCo = activeBodyshop.bodyshops[0].convenient_company; if (convCo === "alpha") { autoUpdater.channel = "alpha"; } else if (convCo === "beta") { autoUpdater.channel = "beta"; } } catch (error) { log.error( "Error while querying active bodyshop or masterdata", errorTypeCheck(error), ); sendIpcToRenderer( ipcTypes.toRenderer.general.showErrorMessage, "Error connecting to ImEX Online servers to get shop data. Please try again.", ); } autoUpdater.checkForUpdatesAndNotify(); }; const ipMainHandleResetPassword = async (): Promise => { shell.openExternal("https://imex.online/resetpassword"); }; export { ipcMainHandleAuthStateChanged, ipMainHandleResetPassword };