Aviso:

Para brindarle información de soporte completa de manera más rápida, el contenido de esta página ha sido traducido al español mediante traducción automática. Para consultar la información de soporte más precisa, consulte la versión en inglés de este contenido.

Codificar la Advanced I/O Function

Comencemos a configurar la Advanced I/O function. Esta función se usará para comunicarse con el cliente y configurará un Dynamic Cron. Los metadatos del Cron se obtendrán usando los datos ingresados en el cliente con la ayuda del SDK de creación de Cron.

La Advanced I/O function presente en el directorio de funciones, BirthdayGreetings/functions/advance_function/ contiene:

  • El archivo principal de la función index.js
  • El archivo de configuración catalyst-config.json
  • Módulos de Node
  • Archivos de dependencias package.json y package-lock.json.

Agregarás código en el archivo index.js.

Para codificar esta función, usarás el framework Express de Node.js. Para importar el paquete Express en el código, debes instalar las dependencias de Express en tu sistema.

Para instalar Express.js en tu máquina local, navega a BirthdayGreetings/functions/advance_function/ en tu terminal y ejecuta el siguiente comando:

copy
$
npm install express --save

catalyst_tutorials_jobscheduling_advacned_func_express

Copia el código dado a continuación y pégalo en el archivo index.js.

Nota: Por favor revisa el código en esta sección para asegurarte de que lo comprendes completamente.
index.js
copy
'use strict';
const catalyst = require('zcatalyst-sdk-node');
const express = require('express');
const app = express();
app.use(express.json());
app.post('/insertReminder', async (req, res) => {
    console.log("Request body:", req.body);
    try {
        const catalystApp = catalyst.initialize(req);
        let userManagement = catalystApp.userManagement();
        let userPromise = userManagement.getCurrentUser();
        userPromise.then(async (currentUser) => {
            console.log(currentUser);
            const datastores = catalystApp.datastore();
            const table = datastores.table('12096000001771061'); // Tu Table ID
            await insertReminder(req.body, table, catalystApp);
            res.status(200).json({
                status: 200,
                message: "Reminder added successfully"
            });
        });
    } catch (error) {
        console.error('Error adding reminder:', error);
        res.status(500).json({
            Error: error.message
        });
    }
});
app.get('/getReminder', async (req, res) => {
    try {
        const catalystApp = catalyst.initialize(req);
        const datastores = catalystApp.datastore();
        const table = datastores.table('12096000001771061'); // Tu Table ID
        const reminders = await getReminders(catalystApp, table);
        res.status(200).json(reminders);
    } catch (error) {
        console.error('Error retrieving reminders:', error);
        res.status(500).json({
            Error: error.message
        });
    }
});
app.put('/updateReminder', async (req, res) => {
    try {
        const catalystApp = catalyst.initialize(req);
        const datastores = catalystApp.datastore();
        const table = datastores.table('12096000001771061'); // Tu Table ID
        await updateReminder(req.body, table, catalystApp);
        res.status(200).json({
            status: 200,
            message: "Reminder updated successfully"
        });
    } catch (error) {
        console.error('Error updating reminder:', error);
        res.status(500).json({
            Error: error.message
        });
    }
});
app.patch('/toggleAutoSend', async (req, res) => {
    try {
        const catalystApp = catalyst.initialize(req);
        const datastores = catalystApp.datastore();
        const table = datastores.table('12096000001771061'); // Tu Table ID
        await toggleAutoSend(req.body, table, catalystApp);
        res.status(200).json({
            status: 200,
            message: "Auto send toggled status successfully"
        });
    } catch (error) {
        console.error('Error toggling status:', error);
        res.status(500).json({
            Error: error.message
        });
    }
});
app.delete('/deleteReminder/:id', async (req, res) => {
    try {
        const {
            id
        } = req.params;
        console.log("ID received:", id);
        const catalystApp = catalyst.initialize(req);
        const datastores = catalystApp.datastore();
        const table = datastores.table('12096000001771061'); // Tu Table ID
        await deleteReminder(id, table, catalystApp);
        res.status(200).json({
            status: 200,
            message: "Reminder deleted successfully"
        });
    } catch (error) {
        console.error('Error deleting reminder:', error);
        res.status(500).json({
            Error: error.message
        });
    }
});
module.exports = app;
async function toggleAutoSend(requestBody, table, catalystApp) {
    const {
        id,
        status
    } = requestBody;
    const enableStatus = status === 'enable';
    const zcql = catalystApp.zcql();
    const query = `SELECT * FROM BirthDayReminder WHERE ROWID = '${id}'`;
    const response = await zcql.executeZCQLQuery(query);
    if (!response || response.length === 0) throw new Error('Reminder not found.');
    const row = response[0].BirthDayReminder;
    row.AutoSend = enableStatus;
    console.log("Row.autosend  ", row.AutoSend);
    const jobScheduling = catalystApp.jobScheduling();
    if (!enableStatus) {
        console.log("false");
        await jobScheduling.CRON.deleteCron(`bday_${id.substring(10, 17)}`);
    } else {
        console.log("true");
        await scheduleCronJob(row, catalystApp);
    }
    const updateQuery = `UPDATE BirthDayReminder SET AutoSend = ${enableStatus} WHERE ROWID = '${id}'`;
    await zcql.executeZCQLQuery(updateQuery);
}
async function insertReminder(requestBody, table, catalystApp) {
    const {
        name,
        birthday,
        message,
        email
    } = requestBody;
    if (!name || !birthday || !message || !email) throw new Error('Missing required fields.');
    const row = {
        Name: name,
        BirthDay: birthday,
        Message: message,
        Email: email
    };
    try {
        const insertedRow = await table.insertRow(row);
        await scheduleCronJob(insertedRow, catalystApp);
    } catch (error) {
        console.error('Error inserting reminder:', error);
        throw error;
    }
}
async function getReminders(catalystApp, table) {
    try {
        const zcql = catalystApp.zcql();
        const query = 'SELECT * FROM BirthDayReminder';
        const response = await zcql.executeZCQLQuery(query);
        if (!Array.isArray(response)) throw new Error('Invalid response from ZCQL query');
        return response.map(row => {
            const reminder = row.BirthDayReminder;
            return {
                ID: reminder.ROWID || 'N/A',
                Name: reminder.Name || 'N/A',
                BirthDay: reminder.BirthDay || 'N/A',
                Message: reminder.Message || 'N/A',
                Email: reminder.Email || 'N/A',
                AutoSend: reminder.AutoSend || false
            };
        });
    } catch (error) {
        console.error('Error retrieving reminders:', error.message);
        throw error;
    }
}
async function updateReminder(requestBody, table, catalystApp) {
    const {
        ID,
        Name,
        BirthDay,
        Message,
        Email
    } = requestBody;
    const jobScheduling = catalystApp.jobScheduling();
    if (!ID || !Name || !BirthDay || !Message || !Email) throw new Error('Missing required fields.');
    const zcql = catalystApp.zcql();
    const query = `SELECT AutoSend FROM BirthDayReminder WHERE ROWID = '${ID}'`;
    const response = await zcql.executeZCQLQuery(query);
    const AutoSend = response[0]?.BirthDayReminder?.AutoSend || false;
    const row = {
        ROWID: ID,
        Name: Name,
        BirthDay: BirthDay,
        Message: Message,
        Email: Email
    };
    if (AutoSend) {
        await jobScheduling.CRON.deleteCron(`bday_${ID.substring(10, 17)}`);
        try {
            await table.updateRow(row);
            await scheduleCronJob(row, catalystApp);
        } catch (error) {
            console.error('Error updating reminder:', error);
            throw error;
        }
    } else {
        try {
            await table.updateRow(row);
            console.log('Row updated:', row);
        } catch (error) {
            console.error('Error updating reminder:', error);
            throw error;
        }
    }
}
async function deleteReminder(id, table, catalystApp) {
    if (!id) throw new Error('ID is missing.');
    const zcql = catalystApp.zcql();
    const query = `SELECT AutoSend FROM BirthDayReminder WHERE ROWID = '${id}'`;
    const response = await zcql.executeZCQLQuery(query);
    const AutoSend = response[0]?.BirthDayReminder?.AutoSend || false;
    const jobScheduling = catalystApp.jobScheduling();
    if (AutoSend) {
        await jobScheduling.CRON.deleteCron(`bday_${id.substring(10, 17)}`);
    }
    await table.deleteRow(id);
    console.log('Reminder deleted:', id);
}
async function scheduleCronJob(row, catalystApp) {
    const jobScheduling = catalystApp.jobScheduling();
    const {
        ROWID,
        BirthDay,
        Name,
        Email,
        Message
    } = row;
    const dob = new Date(BirthDay);
    const cronDetail = {
        cron_name: `bday_${ROWID.substring(10, 17)}`,
        description: `Birthday reminder for ${Name}`,
        cron_status: true,
        cron_type: 'CALENDAR',
        cron_detail: {
            hour: 0,
            minute: 0,
            second: 0,
            days: [dob.getDate()],
            months: [dob.getMonth()],
            repetition_type: 'YEARLY'
        },
        job_meta: {
            job_name: `bday_${ROWID.substring(10, 17)}`,
            jobpool_name: 'Trigger_Message', // Nombre de tu job pool
            jobpool_id: '12096000001771827', // Tu Job pool ID
            target_type: 'FUNCTION',
            target_name: 'dynamic_cron', // Nombre de tu job function
            job_service_identifier: 'default',
            retries: 2,
            retry_interval: 900,
            params: {
                id: ROWID,
                name: Name,
                email: Email,
                message: Message,
                birthday: BirthDay
            }
        }
    };
    try {
        const yearlyCronDetails = await jobScheduling.CRON.createCron(cronDetail);
        console.log('Yearly cron created:', yearlyCronDetails);
    } catch (error) {
        console.error('Error scheduling cron job:', error);
        throw error;
    }
}
View more
Notas:
  • Asegúrate de proporcionar tu Table ID en las líneas 15, 33, 47, 64 y 85. Puedes obtener este valor de la tabla que creaste desde el Data Store.

  • Asegúrate de proporcionar los detalles de tu job meta en las líneas 245, 246 y 248. Puedes obtenerlos de la sección Job Pool en Job Scheduling.

Repasemos las APIs que se usaron para codificar la Advanced I/O function para manejar el enrutamiento entre el servidor, el Data Store y el servicio Job Scheduling:

  • POST /insertReminder: Agregará un nuevo recordatorio al Data Store con los detalles proporcionados en el cliente. También programará un dynamic cron job para el recordatorio usando el servicio Catalyst Job Scheduling para automatizar tareas.

  • GET /getReminder: Recuperará todos los recordatorios almacenados en el Data Store y los devolverá como respuesta.

  • PUT /updateReminder: Actualizará un recordatorio existente en el Data Store con los detalles proporcionados en el cliente.

  • PATCH /toggleAutoSend: Alternará el estado de envío automático de un recordatorio basándose en la preferencia establecida por el usuario en el lado del cliente.

  • DELETE /deleteReminder/:id: Eliminará un recordatorio específico del Data Store y del Cron usando el reminder ID proporcionado.

Última actualización 2026-03-20 21:51:56 +0530 IST

ENLACES RELACIONADOS

Advanced I/O Functions Node.js SDK