mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-24 09:28:31 +00:00
Added apps subscriber to listen entity events
This commit is contained in:
parent
c3cd2c2f2f
commit
ea37a024e0
5 changed files with 51 additions and 5 deletions
38
server/src/entity-subscribers/apps.subscriber.ts
Normal file
38
server/src/entity-subscribers/apps.subscriber.ts
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
import { DataSource, EntitySubscriberInterface, EventSubscriber, InsertEvent } from 'typeorm';
|
||||||
|
import { Repository } from 'typeorm';
|
||||||
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
import { AppVersion } from 'src/entities/app_version.entity';
|
||||||
|
import { App } from 'src/entities/app.entity';
|
||||||
|
|
||||||
|
@EventSubscriber()
|
||||||
|
export class AppsSubscriber implements EntitySubscriberInterface<App> {
|
||||||
|
constructor(
|
||||||
|
dataSource: DataSource,
|
||||||
|
@InjectRepository(AppVersion)
|
||||||
|
private readonly appVersionRepository: Repository<AppVersion>,
|
||||||
|
@InjectRepository(App)
|
||||||
|
private readonly appRepository: Repository<App>
|
||||||
|
) {
|
||||||
|
dataSource.subscribers.push(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
listenTo() {
|
||||||
|
return App;
|
||||||
|
}
|
||||||
|
|
||||||
|
async afterInsert(event: InsertEvent<App>): Promise<void> {
|
||||||
|
const app = event.entity;
|
||||||
|
if (!app.slug) {
|
||||||
|
await this.appRepository.update(app.id, { slug: app.id });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async afterLoad(app: App): Promise<void> {
|
||||||
|
if (app) {
|
||||||
|
app.editingVersion = await this.appVersionRepository.findOne({
|
||||||
|
where: { appId: app.id },
|
||||||
|
order: { updatedAt: 'DESC' },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -72,7 +72,9 @@ export function lowercaseString(value: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const updateTimestampForAppVersion = async (manager, appVersionId) => {
|
export const updateTimestampForAppVersion = async (manager, appVersionId) => {
|
||||||
const appVersion = await manager.findOne('app_versions', appVersionId);
|
const appVersion = await manager.findOne('app_versions', {
|
||||||
|
where: { id: appVersionId },
|
||||||
|
});
|
||||||
if (appVersion) {
|
if (appVersion) {
|
||||||
await manager.update('app_versions', appVersionId, { updatedAt: new Date() });
|
await manager.update('app_versions', appVersionId, { updatedAt: new Date() });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,7 @@ import { ComponentsService } from '@services/components.service';
|
||||||
import { PageService } from '@services/page.service';
|
import { PageService } from '@services/page.service';
|
||||||
import { EventsService } from '@services/events_handler.service';
|
import { EventsService } from '@services/events_handler.service';
|
||||||
import { TooljetDbModule } from '../tooljet_db/tooljet_db.module';
|
import { TooljetDbModule } from '../tooljet_db/tooljet_db.module';
|
||||||
|
import { AppsSubscriber } from 'src/entity-subscribers/apps.subscriber';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
|
|
@ -87,6 +88,7 @@ import { TooljetDbModule } from '../tooljet_db/tooljet_db.module';
|
||||||
ComponentsService,
|
ComponentsService,
|
||||||
PageService,
|
PageService,
|
||||||
EventsService,
|
EventsService,
|
||||||
|
AppsSubscriber,
|
||||||
],
|
],
|
||||||
controllers: [AppsController, AppsControllerV2, AppUsersController, AppsImportExportController],
|
controllers: [AppsController, AppsControllerV2, AppUsersController, AppsImportExportController],
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -68,11 +68,13 @@ export class ComponentsService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(componentDiff: object, appVersionId: string) {
|
async update(componentDiff: object, appVersionId: string) {
|
||||||
return dbTransactionForAppVersionAssociationsUpdate(async (manager) => {
|
return dbTransactionForAppVersionAssociationsUpdate(async (manager:EntityManager) => {
|
||||||
for (const componentId in componentDiff) {
|
for (const componentId in componentDiff) {
|
||||||
const { component } = componentDiff[componentId];
|
const { component } = componentDiff[componentId];
|
||||||
|
|
||||||
const componentData: Component = await manager.findOne(Component, componentId);
|
const componentData: Component = await manager.findOne(Component, {
|
||||||
|
where: { id: componentId }
|
||||||
|
});
|
||||||
|
|
||||||
if (!componentData) {
|
if (!componentData) {
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
|
|
@ -55,8 +55,10 @@ export class PageService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async clonePage(pageId: string, appVersionId: string) {
|
async clonePage(pageId: string, appVersionId: string) {
|
||||||
return dbTransactionForAppVersionAssociationsUpdate(async (manager) => {
|
return dbTransactionForAppVersionAssociationsUpdate(async (manager:EntityManager) => {
|
||||||
const pageToClone = await manager.findOne(Page, pageId);
|
const pageToClone = await manager.findOne(Page, {
|
||||||
|
where: {id:pageId}
|
||||||
|
});
|
||||||
|
|
||||||
if (!pageToClone) {
|
if (!pageToClone) {
|
||||||
throw new Error('Page not found');
|
throw new Error('Page not found');
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue