/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import CONFIG from '@/config' import connection from '@/typeorm/connection' import { checkDBVersion } from '@/typeorm/DBVersion' import { initialize } from '@dbTools/helpers' import { entities } from '@entity/index' import { logger } from './testSetup' export const headerPushMock = jest.fn((t) => { context.token = t.value }) const context = { token: '', setHeaders: { push: headerPushMock, forEach: jest.fn(), }, clientTimezoneOffset: 0, } export const cleanDB = async () => { // this only works as lond we do not have foreign key constraints for (let i = 0; i < entities.length; i++) { await resetEntity(entities[i]) } } export const testEnvironment = async (testLogger: any = logger) => { // open mysql connection const con = await connection() if (!con || !con.isConnected) { logger.fatal(`Couldn't open connection to database!`) throw new Error(`Fatal: Couldn't open connection to database`) } // check for correct database version const dbVersion = await checkDBVersion(CONFIG.DB_VERSION) if (!dbVersion) { logger.fatal('Fatal: Database Version incorrect') throw new Error('Fatal: Database Version incorrect') } await initialize() return { con } } export const resetEntity = async (entity: any) => { const items = await entity.find({ withDeleted: true }) if (items.length > 0) { const ids = items.map((i: any) => i.id) await entity.delete(ids) } } export const resetToken = () => { context.token = '' } // format date string as it comes from the frontend for the contribution date export const contributionDateFormatter = (date: Date): string => { return `${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}` } export const setClientTimezoneOffset = (offset: number): void => { context.clientTimezoneOffset = offset }