|
| 1 | +import { LightningElement, api, wire } from "lwc"; |
| 2 | +import fetchJSFromCmdt from '@salesforce/apex/DynamicSOQLDMLController.getJSFromCmdt'; |
| 3 | +import executeSoql from '@salesforce/apex/DynamicSOQLDMLController.executeSoqlQuery'; |
| 4 | +import runDml from '@salesforce/apex/DynamicSOQLDMLController.executeDml'; |
| 5 | +import getSObjectType from '@salesforce/apex/DynamicSOQLDMLController.getSObjectTypeFromId'; |
| 6 | + |
| 7 | +const REGEX_SOQL = "\\|\\|\\s?(select\\s+[^|]+)\\s?\\|\\|"; |
| 8 | +const REGEX_UPDATE = "\\|\\|\\s?update\\s([^|;]+);?\\s*\\|\\|"; |
| 9 | +const REGEX_INSERT_UPSERT = |
| 10 | + "\\|\\|\\s?(insert|upsert)\\s([\\w\\d_]+)\\s?\\(\\s?(\\w+).*\\|\\|"; |
| 11 | +const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor; |
| 12 | +export default class JsButtonLwc extends LightningElement { |
| 13 | + @api js; |
| 14 | + @api cmdtName; |
| 15 | + @api recordId; |
| 16 | + |
| 17 | + @api |
| 18 | + async invoke() { |
| 19 | + if (!this.js && this.cmdtName) { |
| 20 | + this.js = await fetchJSFromCmdt({ cmdtName }); |
| 21 | + return await this.runJS(); |
| 22 | + } else if (this.js) { |
| 23 | + return await this.runJS(); |
| 24 | + } |
| 25 | + throw Error("No script found to execute"); |
| 26 | + } |
| 27 | + |
| 28 | + executeSoql(){ |
| 29 | + //TODO: implement |
| 30 | + } |
| 31 | + |
| 32 | + executeDml(){ |
| 33 | + //TODO: implement |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + runJS() { |
| 38 | + //replace consecutive spaces |
| 39 | + this.js = this.js.replace(/\s+/g, " "); |
| 40 | + |
| 41 | + //parse soql |
| 42 | + this.js = this.js.replace( |
| 43 | + new RegExp(helper.REGEX_SOQL, "gi"), |
| 44 | + "await this.executeSoql(cmp,`$1`);" |
| 45 | + ); |
| 46 | + |
| 47 | + //parse updates |
| 48 | + this.js = this.js.replace( |
| 49 | + new RegExp(helper.REGEX_UPDATE, "gi"), |
| 50 | + "await this.executeDml(cmp,'update',$1);" |
| 51 | + ); |
| 52 | + |
| 53 | + //parse inserts |
| 54 | + this.js = this.js.replace( |
| 55 | + new RegExp(helper.REGEX_INSERT_UPSERT, "gi"), |
| 56 | + "await this.executeDml(cmp,'$1',$3,'$2');" |
| 57 | + ); |
| 58 | + |
| 59 | + return await AsyncFunction("recordId", `return ${js}`).bind(this)(recordId); |
| 60 | + |
| 61 | + } |
| 62 | +} |
0 commit comments