@seratch_/bolt-fastify
Version:
Bolt for JavaScript Extension - Fastify
246 lines • 12.2 kB
JavaScript
;
/* eslint-disable import/prefer-default-export */
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("@slack/logger");
const sequelize_1 = require("sequelize");
const SlackAppInstallation_1 = __importDefault(require("./SlackAppInstallation"));
class SequelizeInstallationStore {
constructor(options) {
this.sequelize = options.sequelize;
this.clientId = options.clientId;
this.logger = options.logger !== undefined ?
options.logger : new logger_1.ConsoleLogger();
this.historicalDataEnabled = options.historicalDataEnabled !== undefined ?
options.historicalDataEnabled : true;
if (options.model !== undefined) {
// If you want to customize the model initialization,
// you can do so before passing this options.model to this constructor.
this.model = options.model;
}
else {
this.model = SlackAppInstallation_1.default;
this.model.init(SlackAppInstallation_1.default.buildNewModelAttributes(), { sequelize: this.sequelize, modelName: 'slack_app_installation' });
}
this.onStoreInstallation = options.onStoreInstallation !== undefined ?
options.onStoreInstallation : async (_) => { };
this.onFetchInstallation = options.onFetchInstallation !== undefined ?
options.onFetchInstallation : async (_) => { };
this.onDeleteInstallation = options.onDeleteInstallation !== undefined ?
options.onDeleteInstallation : async (_) => { };
this.logger.debug(`SequelizeInstallationStore has been initialized (clientId: ${this.clientId}, model: ${this.model.name})`);
}
async storeInstallation(i, logger) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
const enterpriseId = (_a = i.enterprise) === null || _a === void 0 ? void 0 : _a.id;
const teamId = (_b = i.team) === null || _b === void 0 ? void 0 : _b.id;
const userId = i.user.id;
const isEnterpriseInstall = i.isEnterpriseInstall || false;
const commonLogPart = `(enterprise_id: ${enterpriseId}, team_id: ${teamId}, user_id: ${userId})`;
logger === null || logger === void 0 ? void 0 : logger.debug(`#storeInstallation starts ${commonLogPart}`);
await this.sequelize.sync();
const entity = {
clientId: this.clientId,
appId: i.appId,
enterpriseId: (_c = i.enterprise) === null || _c === void 0 ? void 0 : _c.id,
enterpriseName: (_d = i.enterprise) === null || _d === void 0 ? void 0 : _d.name,
enterpriseUrl: i.enterpriseUrl,
teamId: (_e = i.team) === null || _e === void 0 ? void 0 : _e.id,
teamName: (_f = i.team) === null || _f === void 0 ? void 0 : _f.name,
botToken: (_g = i.bot) === null || _g === void 0 ? void 0 : _g.token,
botId: (_h = i.bot) === null || _h === void 0 ? void 0 : _h.id,
botUserId: (_j = i.bot) === null || _j === void 0 ? void 0 : _j.userId,
botScopes: (_l = (_k = i.bot) === null || _k === void 0 ? void 0 : _k.scopes) === null || _l === void 0 ? void 0 : _l.join(','),
botRefreshToken: (_m = i.bot) === null || _m === void 0 ? void 0 : _m.refreshToken,
botTokenExpiresAt: ((_o = i.bot) === null || _o === void 0 ? void 0 : _o.expiresAt) ?
new Date(i.bot.expiresAt * 1000) :
undefined,
userId: i.user.id,
userToken: i.user.token,
userScopes: (_p = i.user.scopes) === null || _p === void 0 ? void 0 : _p.join(','),
userRefreshToken: i.user.refreshToken,
userTokenExpiresAt: ((_q = i.user) === null || _q === void 0 ? void 0 : _q.expiresAt) ?
new Date(i.user.expiresAt * 1000) :
undefined,
incomingWebhookUrl: (_r = i.incomingWebhook) === null || _r === void 0 ? void 0 : _r.url,
incomingWebhookChannel: (_s = i.incomingWebhook) === null || _s === void 0 ? void 0 : _s.channel,
incomingWebhookChannelId: (_t = i.incomingWebhook) === null || _t === void 0 ? void 0 : _t.channelId,
incomingWebhookConfigurationUrl: (_u = i.incomingWebhook) === null || _u === void 0 ? void 0 : _u.configurationUrl,
isEnterpriseInstall: i.isEnterpriseInstall,
tokenType: i.tokenType,
installedAt: new Date(),
};
if (this.historicalDataEnabled) {
await this.onStoreInstallation({
model: entity,
installation: i,
logger: this.logger,
});
await this.model.create(entity);
}
else {
const where = this.buildFullWhereClause({
enterpriseId,
teamId,
userId,
isEnterpriseInstall,
});
await this.onStoreInstallation({
model: entity,
installation: i,
logger: this.logger,
query: where,
});
const [count] = await this.model.update(entity, { where });
if (count === 0) {
await this.model.create(entity);
}
}
logger === null || logger === void 0 ? void 0 : logger.debug(`#storeInstallation successfully completed ${commonLogPart}`);
}
async fetchInstallation(query, logger) {
var _a, _b;
const { enterpriseId, teamId, userId } = query;
const commonLogPart = `(enterprise_id: ${enterpriseId}, team_id: ${teamId}, user_id: ${userId})`;
logger === null || logger === void 0 ? void 0 : logger.debug(`#fetchInstallation starts ${commonLogPart}`);
await this.sequelize.sync();
// If query.userId is present, the latest user associated installation will be fetched
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const where = this.buildFullWhereClause(query);
let row = await this.model.findOne({
where,
order: [['id', 'DESC']],
limit: 1,
});
if (query.userId !== undefined) {
// Fetch the latest bot data in the table
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const botWhere = this.buildBotQuery(query);
const botRow = await this.model.findOne({ where: botWhere, order: [['id', 'DESC']], limit: 1 });
if (botRow && botRow.botId) {
if (row) {
row.botId = botRow.botId;
row.botRefreshToken = botRow.botRefreshToken;
row.botScopes = botRow.botScopes;
row.botToken = botRow.botToken;
row.botTokenExpiresAt = botRow.botTokenExpiresAt;
row.botUserId = botRow.botUserId;
}
else {
row = botRow;
}
}
}
if (row) {
logger === null || logger === void 0 ? void 0 : logger.debug(`#fetchInstallation found the installation data ${commonLogPart}`);
const installation = {
team: row.teamId ?
{
id: row.teamId,
name: row.teamName,
} :
undefined,
enterprise: row.enterpriseId ?
{
id: row.enterpriseId,
name: row.enterpriseName,
} :
undefined,
enterpriseUrl: row.enterpriseUrl,
user: {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
id: row.userId,
token: row.userToken,
refreshToken: row.userRefreshToken,
expiresAt: row.userTokenExpiresAt ? Math.floor(row.userTokenExpiresAt.getTime() / 1000) : undefined,
scopes: (_a = row.userScopes) === null || _a === void 0 ? void 0 : _a.split(','),
},
bot: row.botId && row.botUserId && row.botToken ?
{
id: row.botId,
userId: row.botUserId,
token: row.botToken,
refreshToken: row.botRefreshToken,
expiresAt: row.botTokenExpiresAt ? Math.floor(row.botTokenExpiresAt.getTime() / 1000) : undefined,
scopes: ((_b = row.botScopes) === null || _b === void 0 ? void 0 : _b.split(',')) || [],
} :
undefined,
incomingWebhook: row.incomingWebhookUrl ?
{
url: row.incomingWebhookUrl,
channel: row.incomingWebhookChannel,
channelId: row.incomingWebhookChannelId,
configurationUrl: row.incomingWebhookConfigurationUrl,
} :
undefined,
appId: row.appId,
tokenType: 'bot',
isEnterpriseInstall: row.isEnterpriseInstall,
authVersion: 'v2', // This module does not support v1 installations
};
await this.onFetchInstallation({
query,
installation,
model: row,
logger: this.logger,
});
return installation;
}
logger === null || logger === void 0 ? void 0 : logger.debug(`#fetchInstallation didn't return any installation data ${commonLogPart}`);
throw new Error(`No installation data found ${commonLogPart}`);
}
async deleteInstallation(query, logger) {
const { enterpriseId, teamId, userId } = query;
const commonLogPart = `(enterprise_id: ${enterpriseId}, team_id: ${teamId}, user_id: ${userId})`;
logger === null || logger === void 0 ? void 0 : logger.debug(`#deleteInstallation starts ${commonLogPart}`);
await this.sequelize.sync();
await this.onDeleteInstallation({
query,
logger: this.logger,
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const where = this.buildFullWhereClause(query);
const deletionCount = await this.model.destroy({ where });
logger === null || logger === void 0 ? void 0 : logger.debug(`#deleteInstallation deleted ${deletionCount} rows ${commonLogPart}`);
}
// eslint-disable-next-line class-methods-use-this
async close() {
await this.sequelize.close();
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
buildBotQuery(query) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const where = this.buildFullWhereClause(query);
// No userId here
delete where.userId;
return where;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
buildFullWhereClause(query) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const where = {};
if (this.clientId !== undefined) {
where.clientId = this.clientId;
}
else {
where.clientId = { [sequelize_1.Op.eq]: null };
}
if (query.enterpriseId !== undefined) {
where.enterpriseId = query.enterpriseId;
}
if (query.isEnterpriseInstall) {
where.teamId = { [sequelize_1.Op.eq]: null };
}
else if (query.teamId !== undefined) {
where.teamId = query.teamId;
}
if (query.userId !== undefined) {
where.userId = query.userId;
}
return where;
}
}
exports.default = SequelizeInstallationStore;
//# sourceMappingURL=SequelizeInstallationStore.js.map