2022-07-19 19:56:48 +00:00
|
|
|
import { Injectable, Inject } from '@nestjs/common';
|
2022-06-29 10:12:27 +00:00
|
|
|
import { Model } from 'mongoose';
|
2022-07-19 19:56:48 +00:00
|
|
|
import { Community, CommunityDocument } from 'src/schemas/community.schema';
|
2022-06-29 10:12:27 +00:00
|
|
|
import { InjectModel } from '@nestjs/mongoose';
|
2022-07-19 19:56:48 +00:00
|
|
|
import { RpcException, ClientProxy } from '@nestjs/microservices';
|
|
|
|
import { from, lastValueFrom, map, scan, mergeMap } from 'rxjs';
|
|
|
|
import { Admin } from 'src/schemas/admin.entity';
|
|
|
|
import { appendFileSync } from 'fs';
|
|
|
|
|
2022-06-29 10:12:27 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class CommunitiesService {
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
@InjectModel(Community.name) private readonly communityModel: Model<CommunityDocument>,
|
2022-07-19 19:56:48 +00:00
|
|
|
@Inject('SERVICIO_USUARIOS') private readonly clientUserApp: ClientProxy,
|
|
|
|
|
|
|
|
) { }
|
2022-06-29 10:12:27 +00:00
|
|
|
|
|
|
|
async create(community: CommunityDocument): Promise<Community> {
|
|
|
|
return this.communityModel.create(community);
|
|
|
|
}
|
|
|
|
|
2022-07-19 19:56:48 +00:00
|
|
|
async findAll(): Promise<Community[]> {
|
|
|
|
|
|
|
|
return await this.communityModel
|
|
|
|
.find()
|
|
|
|
.setOptions({ sanitizeFilter: true })
|
|
|
|
.exec()
|
2022-07-21 06:15:43 +00:00
|
|
|
.then(async community => {
|
|
|
|
if (community) {
|
2022-07-19 19:56:48 +00:00
|
|
|
await Promise.all(community.map(async c => {
|
2022-07-21 06:15:43 +00:00
|
|
|
//buscar al usuario con el id de la comunidad anexado
|
2022-07-19 19:56:48 +00:00
|
|
|
let admin = await this.findCommunityAdmin(c["_id"], "2")
|
2022-07-21 06:15:43 +00:00
|
|
|
if (admin) {
|
2022-07-19 19:56:48 +00:00
|
|
|
c["id_admin"] = admin["_id"]
|
|
|
|
c["name_admin"] = admin["name"]
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}))
|
|
|
|
}
|
2022-07-21 06:15:43 +00:00
|
|
|
return community;
|
2022-07-19 19:56:48 +00:00
|
|
|
})
|
2022-06-29 10:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
findOne(id: string): Promise<Community> {
|
|
|
|
return this.communityModel.findOne({ _id: id }).exec();
|
|
|
|
}
|
2022-07-15 03:16:02 +00:00
|
|
|
findOneName(id: string): Promise<Community> {
|
2022-07-24 00:26:50 +00:00
|
|
|
return this.communityModel.findOne({ _id: id }).exec();
|
2022-07-15 03:16:02 +00:00
|
|
|
}
|
2022-06-29 10:12:27 +00:00
|
|
|
|
|
|
|
update(id: string, community: CommunityDocument) {
|
|
|
|
return this.communityModel.findOneAndUpdate({ _id: id }, community, {
|
|
|
|
new: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async remove(id: string) {
|
|
|
|
return this.communityModel.findByIdAndRemove({ _id: id }).exec();
|
|
|
|
}
|
2022-07-19 19:56:48 +00:00
|
|
|
|
|
|
|
async findCommunityAdmin(community: string, user_type: string) {
|
|
|
|
const pattern = { cmd: 'findOneCommunityUser' }
|
|
|
|
const payload = { community_id: community, user_type: user_type }
|
|
|
|
|
|
|
|
let callback = await this.clientUserApp
|
|
|
|
.send<string>(pattern, payload)
|
|
|
|
.pipe(
|
|
|
|
map((response: string) => ({ response }))
|
|
|
|
)
|
|
|
|
|
|
|
|
const finalValue = await lastValueFrom(callback);
|
|
|
|
return finalValue['response'];
|
|
|
|
|
|
|
|
}
|
2022-06-29 10:12:27 +00:00
|
|
|
}
|