mirror of
https://github.com/dadosfera/maestro.git
synced 2026-11-25 06:50:05 +00:00
FIX: connections migration
This commit is contained in:
@@ -8,8 +8,10 @@ import { ClientGrpc } from '@nestjs/microservices';
|
||||
import { ConnectionManager } from '@dadosfera/protospack-v2';
|
||||
import { lastValueFrom } from 'rxjs';
|
||||
import {
|
||||
ConnectionApiConnection,
|
||||
ConnectionRes,
|
||||
ConnectionsRes,
|
||||
ConnectionToCatalogDto,
|
||||
CreateConnectionDto,
|
||||
UpdateConnectionDto,
|
||||
} from './dtos/connection';
|
||||
@@ -18,6 +20,8 @@ import {
|
||||
GetConnectionDetailsResponse,
|
||||
INTERNAL_GetAllConnectionsResponse,
|
||||
} from '@dadosfera/protospack-v2/dist/lib/ConnectionManager/interfaces/messages';
|
||||
import { UsersService } from '../users/users.service';
|
||||
import { ConnectorClientService } from '../connector/client.service';
|
||||
|
||||
export class ConnectionClientService implements OnModuleInit {
|
||||
private connectionServiceRead: ConnectionManager.ReadService.ConnectionManagerReadServices;
|
||||
@@ -25,6 +29,8 @@ export class ConnectionClientService implements OnModuleInit {
|
||||
constructor(
|
||||
@Inject(ConnectionClientConfiguration.name)
|
||||
private readonly grpcClient: ClientGrpc,
|
||||
private usersService: UsersService,
|
||||
private connectorService: ConnectorClientService,
|
||||
) {}
|
||||
|
||||
onModuleInit() {
|
||||
@@ -163,4 +169,105 @@ export class ConnectionClientService implements OnModuleInit {
|
||||
this.connectionServiceRead.INTERNAL_GetAllConnections({ customer_name }),
|
||||
);
|
||||
}
|
||||
|
||||
async catalogExistingConnection(data: ConnectionToCatalogDto) {
|
||||
return lastValueFrom(
|
||||
this.connectionServiceRead.INTERNAL_CatalogExistingConnection({
|
||||
data_str: JSON.stringify(data),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async synchronizeConnections(apply: boolean) {
|
||||
const connectors = await this.connectorService
|
||||
.getAllConnectors({
|
||||
search: undefined,
|
||||
filters: undefined,
|
||||
language: 'pt-br',
|
||||
page: 1,
|
||||
size: 500,
|
||||
})
|
||||
.then(({ connectors: connectorsStr }) =>
|
||||
JSON.parse(connectorsStr).map((c) => c),
|
||||
);
|
||||
const { customers } = await this.usersService.findaAllCustomers();
|
||||
const promises: Promise<{
|
||||
customer_name: string;
|
||||
customer_id: string;
|
||||
productConnections: Record<string, any>;
|
||||
platformConnections: ConnectionApiConnection[];
|
||||
}>[] = [];
|
||||
customers.forEach((c) => {
|
||||
const customer_name = c.name;
|
||||
const customer_id = c.id;
|
||||
promises.push(
|
||||
this.INTERNAL_getAllConnections({ customer_name }).then((res) => ({
|
||||
customer_name,
|
||||
customer_id,
|
||||
platformConnections: JSON.parse(res.platformConnections),
|
||||
productConnections: JSON.parse(res.productConnections),
|
||||
})),
|
||||
);
|
||||
});
|
||||
const customer_connections = await Promise.all(promises);
|
||||
|
||||
const missingInProduct: ConnectionApiConnection[] = [];
|
||||
const migrations = customer_connections.map((cc) => {
|
||||
const {
|
||||
customer_name,
|
||||
platformConnections,
|
||||
productConnections,
|
||||
customer_id,
|
||||
} = cc;
|
||||
|
||||
platformConnections.forEach((plc) => {
|
||||
const productConnection = productConnections.find(
|
||||
(prc) => prc.id === plc.config_id,
|
||||
);
|
||||
if (!productConnection) missingInProduct.push(plc);
|
||||
});
|
||||
|
||||
const updatePromises: Promise<any>[] = [];
|
||||
for (const connection of missingInProduct) {
|
||||
const connector = connectors.find(
|
||||
(c) => c._source.plugin === connection.plugin,
|
||||
);
|
||||
const connectionToCatalog = {
|
||||
id: connection.config_id,
|
||||
created_at: connection.created_at,
|
||||
updated_at: new Date().toISOString(),
|
||||
customer_id: customer_id,
|
||||
user_id: '',
|
||||
plugin: connection.plugin,
|
||||
name: connection.name,
|
||||
description: connection.description,
|
||||
image_url: connector._source.image,
|
||||
network_config_id: connection.network_config_id,
|
||||
type: connection.type,
|
||||
connector_id: connector._id,
|
||||
connector_name: connector._source.name,
|
||||
in_use: 1,
|
||||
username: 'auto',
|
||||
customer_name: customer_name,
|
||||
};
|
||||
updatePromises.push(
|
||||
this.catalogExistingConnection(connectionToCatalog),
|
||||
);
|
||||
}
|
||||
return { customer: { ...cc }, updatePromises, missingInProduct };
|
||||
});
|
||||
const customersStatus: any[] = [];
|
||||
for (const migration of migrations) {
|
||||
let status: any[] = [];
|
||||
if (apply) {
|
||||
status = await Promise.all(migration.updatePromises);
|
||||
}
|
||||
customersStatus.push({
|
||||
customer: migration.customer.customer_name,
|
||||
status,
|
||||
missingInProduct: migration.missingInProduct,
|
||||
});
|
||||
}
|
||||
return { customersStatus };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user