import { Ok, Err } from '@nangohq/utils';
import db from '@nangohq/database';
const DB_TABLE = '_nango_active_logs';
export const errorNotificationService = {
    auth: {
        create: async ({ type, action, connection_id, log_id, active }) => {
            return await db.knex.transaction(async (trx) => {
                await errorNotificationService.auth.clear({ connection_id, trx });
                const created = await trx
                    .from(DB_TABLE)
                    .insert({
                    type,
                    action,
                    connection_id,
                    log_id,
                    active
                })
                    .returning('*');
                if (created?.[0]) {
                    return Ok(created[0]);
                }
                else {
                    return Err('Failed to create notification');
                }
            });
        },
        get: async (id) => {
            return await db.knex.from(DB_TABLE).where({ type: 'auth', connection_id: id, active: true }).first();
        },
        clear: async ({ connection_id, trx = db.knex }) => {
            await trx.from(DB_TABLE).where({ type: 'auth', connection_id }).delete();
        }
    },
    sync: {
        create: async ({ type, action, sync_id, connection_id, log_id, active }) => {
            return await db.knex.transaction(async (trx) => {
                await errorNotificationService.sync.clear({ sync_id, connection_id, trx });
                const created = await trx
                    .from(DB_TABLE)
                    .insert({
                    type,
                    action,
                    sync_id,
                    connection_id,
                    log_id,
                    active
                })
                    .returning('*');
                if (created?.[0]) {
                    return Ok(created[0]);
                }
                else {
                    return Err('Failed to create notification');
                }
            });
        },
        clear: async ({ sync_id, connection_id, trx = db.knex }) => {
            await trx.from(DB_TABLE).where({ type: 'sync', sync_id, connection_id }).delete();
        },
        clearBySyncId: async ({ sync_id }) => {
            await db.knex.from(DB_TABLE).where({ type: 'sync', sync_id }).delete();
        }
    }
};
//# sourceMappingURL=error.service.js.map