@salesforce/apex-node
Version:
Salesforce JS library for Apex
174 lines • 7.2 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TraceFlags = void 0;
const i18n_1 = require("../i18n");
const logs_1 = require("../logs");
const dateUtil_1 = require("./dateUtil");
const authUtil_1 = require("./authUtil");
const elapsedTime_1 = require("./elapsedTime");
class TraceFlags {
connection;
constructor(connection) {
this.connection = connection;
}
async ensureTraceFlags(debugLevelName) {
const username = this.connection.getUsername();
if (!username) {
throw new Error(i18n_1.nls.localize('error_no_default_username'));
}
const userId = (await this.getUserIdOrThrow(username)).Id;
const traceFlag = await this.getTraceFlagForUser(userId);
if (traceFlag) {
// update existing debug level and trace flag
if (!(await this.updateDebugLevel(traceFlag.DebugLevelId))) {
return false;
}
const expirationDate = this.calculateExpirationDate(traceFlag.ExpirationDate
? new Date(traceFlag.ExpirationDate)
: new Date());
return await this.updateTraceFlag(traceFlag.Id, expirationDate);
}
else {
const debugLevelId = await this.getDebugLevelId(debugLevelName);
// create a trace flag
const expirationDate = this.calculateExpirationDate(new Date());
if (!(await this.createTraceFlag(userId, debugLevelId, expirationDate))) {
return false;
}
}
return true;
}
async getDebugLevelId(debugLevelName) {
let debugLevelId;
if (debugLevelName) {
debugLevelId = await this.findDebugLevel(debugLevelName);
if (!debugLevelId) {
throw new Error(i18n_1.nls.localize('trace_flags_failed_to_find_debug_level', debugLevelName));
}
}
else {
debugLevelId = await this.createDebugLevel(logs_1.DEFAULT_DEBUG_LEVEL_NAME);
if (!debugLevelId) {
throw new Error(i18n_1.nls.localize('trace_flags_failed_to_create_debug_level'));
}
}
return debugLevelId;
}
async findDebugLevel(debugLevelName) {
const escapedDebugLevel = (0, authUtil_1.escapeXml)(debugLevelName);
const query = `SELECT Id FROM DebugLevel WHERE DeveloperName = '${escapedDebugLevel}'`;
const result = (await this.connection.tooling.query(query));
return result.totalSize && result.totalSize > 0 && result.records
? result.records[0].Id
: undefined;
}
async updateDebugLevel(id) {
const debugLevel = {
Id: id,
ApexCode: 'FINEST',
Visualforce: 'FINER'
};
const result = (await this.connection.tooling.update('DebugLevel', debugLevel));
return result.success;
}
async createDebugLevel(debugLevelName) {
const developerName = debugLevelName;
const debugLevel = {
developerName,
MasterLabel: developerName,
ApexCode: 'FINEST',
Visualforce: 'FINER'
};
const result = (await this.connection.tooling.create('DebugLevel', debugLevel));
return result.success && result.id ? result.id : undefined;
}
async updateTraceFlag(id, expirationDate) {
const traceFlag = {
Id: id,
StartDate: Date.now(),
ExpirationDate: expirationDate.toUTCString()
};
const result = (await this.connection.tooling.update('TraceFlag', traceFlag));
return result.success;
}
async createTraceFlag(userId, debugLevelId, expirationDate) {
const traceFlag = {
tracedentityid: userId,
logtype: logs_1.LOG_TYPE,
debuglevelid: debugLevelId,
StartDate: Date.now(),
ExpirationDate: expirationDate.toUTCString()
};
const result = (await this.connection.tooling.create('TraceFlag', traceFlag));
return result.success && result.id ? result.id : undefined;
}
isValidDateLength(expirationDate) {
const currDate = Date.now();
return (expirationDate.getTime() - currDate >
logs_1.LOG_TIMER_LENGTH_MINUTES * dateUtil_1.MILLISECONDS_PER_MINUTE);
}
calculateExpirationDate(expirationDate) {
if (!this.isValidDateLength(expirationDate)) {
expirationDate = new Date(Date.now() + logs_1.LOG_TIMER_LENGTH_MINUTES * dateUtil_1.MILLISECONDS_PER_MINUTE);
}
return expirationDate;
}
async getUserIdOrThrow(username) {
const escapedUsername = (0, authUtil_1.escapeXml)(username);
const userQuery = `SELECT Id FROM User WHERE username='${escapedUsername}'`;
const userResult = await this.connection.query(userQuery);
if (userResult.totalSize === 0) {
throw new Error(i18n_1.nls.localize('trace_flags_unknown_user'));
}
return userResult.records[0];
}
async getTraceFlagForUser(userId) {
const traceFlagQuery = `
SELECT Id, logtype, startdate, expirationdate, debuglevelid, debuglevel.apexcode, debuglevel.visualforce
FROM TraceFlag
WHERE logtype='${logs_1.LOG_TYPE}' AND TracedEntityId='${userId}'
ORDER BY CreatedDate DESC
LIMIT 1
`;
const traceFlagResult = await this.connection.tooling.query(traceFlagQuery);
if (traceFlagResult.totalSize > 0) {
return traceFlagResult.records[0];
}
return undefined;
}
}
exports.TraceFlags = TraceFlags;
__decorate([
(0, elapsedTime_1.elapsedTime)()
], TraceFlags.prototype, "ensureTraceFlags", null);
__decorate([
(0, elapsedTime_1.elapsedTime)()
], TraceFlags.prototype, "getDebugLevelId", null);
__decorate([
(0, elapsedTime_1.elapsedTime)()
], TraceFlags.prototype, "findDebugLevel", null);
__decorate([
(0, elapsedTime_1.elapsedTime)()
], TraceFlags.prototype, "updateDebugLevel", null);
__decorate([
(0, elapsedTime_1.elapsedTime)()
], TraceFlags.prototype, "createDebugLevel", null);
__decorate([
(0, elapsedTime_1.elapsedTime)()
], TraceFlags.prototype, "updateTraceFlag", null);
__decorate([
(0, elapsedTime_1.elapsedTime)()
], TraceFlags.prototype, "createTraceFlag", null);
__decorate([
(0, elapsedTime_1.elapsedTime)()
], TraceFlags.prototype, "getUserIdOrThrow", null);
__decorate([
(0, elapsedTime_1.elapsedTime)()
], TraceFlags.prototype, "getTraceFlagForUser", null);
//# sourceMappingURL=traceFlags.js.map