UNPKG

@daveyplate/supabase-swr-entities

Version:

An entity management library for Supabase and SWR

49 lines (48 loc) 2.93 kB
import { createEntity, getEntity, loadEntitySchema } from "./entity-helpers"; /** * Safely retrieves a nested value from an object given a path string like 'article.user_id'. * @param {object} obj - The object to retrieve a value from. * @param {string} path - The path string, e.g., 'article.user_id'. */ function getNestedValue(obj, path) { return path === null || path === void 0 ? void 0 : path.split('.').reduce((acc, key) => acc && acc[key], obj); } /** * Replaces placeholders in the form of {{variable}} with corresponding values from entity. * @param {string} template - The string template with placeholders. * @param {object} entity - An object containing key-value pairs to replace placeholders. */ function replaceBrackets(template, entity) { return template === null || template === void 0 ? void 0 : template.replace(/{{(\w+(\.\w+)*)}}/g, (match, key) => { return getNestedValue(entity, key) || match; }); } export async function createNotification(table, method, entity) { const { entitySchema: { notifications, notificationTemplate } } = await loadEntitySchema(table); if (!notifications) return; // Use template fields and entity to construct notification JSON const notification = { user_id: getNestedValue(entity, notificationTemplate.userIdColumn), sender_id: getNestedValue(entity, notificationTemplate.senderIdColumn), content: { en: replaceBrackets(notificationTemplate.content.en, entity), }, image_url: getNestedValue(entity, notificationTemplate.imageUrlColumn), url: replaceBrackets(notificationTemplate.url, entity), url_as: replaceBrackets(notificationTemplate.urlAs, entity), primary_label: notificationTemplate.primaryLabel && { en: replaceBrackets(notificationTemplate.primaryLabel.en, entity) }, primary_action: notificationTemplate.primaryAction && Object.assign(Object.assign({}, notificationTemplate.primaryAction), { url: replaceBrackets(notificationTemplate.primaryAction.url, entity), urlAs: replaceBrackets(notificationTemplate.primaryAction.urlAs, entity) }), secondary_label: notificationTemplate.secondaryLabel && { en: replaceBrackets(notificationTemplate.secondaryLabel.en, entity) }, secondary_action: notificationTemplate.secondaryAction && Object.assign(Object.assign({}, notificationTemplate.secondaryAction), { url: replaceBrackets(notificationTemplate.secondaryAction.url, entity), urlAs: replaceBrackets(notificationTemplate.secondaryAction.urlAs, entity) }), }; // Check the user metadata to see if notifications are enabled const { entity: metadata } = await getEntity('metadata', notification.user_id); if (!(metadata === null || metadata === void 0 ? void 0 : metadata.notifications_enabled)) return; await createEntity('notifications', notification); }