(cloudCodeFunction, className, objectIds, payload, formData, showNote, onRefresh, onRefreshObjects)
| 122 | * @param {Function} onRefreshObjects - Callback to refresh specific objects by IDs |
| 123 | */ |
| 124 | export async function executeScriptCallback(cloudCodeFunction, className, objectIds, payload, formData, showNote, onRefresh, onRefreshObjects) { |
| 125 | try { |
| 126 | const objects = objectIds.map(id => |
| 127 | Parse.Object.extend(className).createWithoutData(id) |
| 128 | ); |
| 129 | |
| 130 | const results = await Promise.all( |
| 131 | objects.map(object => |
| 132 | Parse.Cloud.run( |
| 133 | cloudCodeFunction, |
| 134 | { object: object.toPointer(), payload, formData }, |
| 135 | { useMasterKey: true } |
| 136 | ) |
| 137 | .then(response => ({ objectId: object.id, response })) |
| 138 | .catch(error => ({ objectId: object.id, error })) |
| 139 | ) |
| 140 | ); |
| 141 | |
| 142 | let errorCount = 0; |
| 143 | results.forEach(({ objectId, response, error }) => { |
| 144 | if (error) { |
| 145 | errorCount++; |
| 146 | showNote?.(`Error running callback on "${objectId}": ${error.message}`, true); |
| 147 | } else { |
| 148 | const note = |
| 149 | (typeof response === 'object' ? JSON.stringify(response) : response) || |
| 150 | `Ran callback on "${objectId}".`; |
| 151 | showNote?.(note); |
| 152 | } |
| 153 | }); |
| 154 | |
| 155 | if (objectIds.length > 1) { |
| 156 | showNote?.( |
| 157 | `Ran callback on ${objectIds.length} objects with ${errorCount} errors.`, |
| 158 | errorCount > 0 |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | if (onRefreshObjects) { |
| 163 | onRefreshObjects(objectIds); |
| 164 | } else { |
| 165 | onRefresh?.(); |
| 166 | } |
| 167 | } catch (e) { |
| 168 | showNote?.(e.message, true); |
| 169 | console.error(`Could not run callback ${cloudCodeFunction}:`, e); |
| 170 | } |
| 171 | } |
no test coverage detected