* chore: multiple apps * fix: multiple apps with application options * fix: multiple apps AppSelector type * chore: multiple apps with plugin config * chore: rename multiple-apps to multiple-apps-manager * chore: application association * chore: plugin multi-app manager * chore: notifications transaction
86 lines
2.0 KiB
TypeScript
86 lines
2.0 KiB
TypeScript
import Database, { Model } from '@nocobase/database';
|
|
import { NotificationService } from './NotificationService';
|
|
import _ from 'lodash';
|
|
|
|
export class Notification extends Model {
|
|
[key: string]: any;
|
|
|
|
get db(): Database {
|
|
return this.constructor['database'];
|
|
}
|
|
|
|
async getReceiversByOptions(): Promise<any[]> {
|
|
const { data, fromTable, filter, dataField } = this.receiver_options;
|
|
let receivers = [];
|
|
if (data) {
|
|
receivers = Array.isArray(data) ? data : [data];
|
|
} else if (fromTable) {
|
|
const collection = this.db.getCollection(fromTable);
|
|
const rows = await collection.repository.find({
|
|
filter,
|
|
});
|
|
receivers = rows.map((row) => row[dataField]);
|
|
}
|
|
return receivers;
|
|
}
|
|
|
|
async send(options: any = {}) {
|
|
const { transaction } = options;
|
|
|
|
if (!this.service) {
|
|
this.service = await this.getService();
|
|
}
|
|
const receivers = await this.getReceiversByOptions();
|
|
let { to } = options;
|
|
if (to) {
|
|
to = Array.isArray(to) ? to : [to];
|
|
receivers.push(...to);
|
|
}
|
|
console.log(receivers);
|
|
for (const receiver of receivers) {
|
|
try {
|
|
const response = await (this.service as NotificationService).send({
|
|
to: receiver,
|
|
subject: this.getSubject(),
|
|
html: this.getBody(options),
|
|
});
|
|
await this.createLog(
|
|
{
|
|
receiver,
|
|
state: 'success',
|
|
response,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
await new Promise((resolve) => {
|
|
setTimeout(resolve, 100);
|
|
});
|
|
} catch (error) {
|
|
console.error(error);
|
|
await this.createLog(
|
|
{
|
|
receiver,
|
|
state: 'fail',
|
|
response: {},
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
getSubject() {
|
|
return this.subject;
|
|
}
|
|
|
|
getBody(data) {
|
|
const compiled = _.template(this.body);
|
|
const body = compiled(data);
|
|
return body;
|
|
}
|
|
}
|