Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
soyombo-baterdene committed Oct 24, 2023
1 parent f2be70e commit 3068c22
Show file tree
Hide file tree
Showing 20 changed files with 309 additions and 203 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-cards-api/src/messageBroker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const initBroker = async cl => {
consumeRPCQueue('cards:tasks.create', async ({ subdomain, data }) => {
const models = await generateModels(subdomain);

const task = await models.Tasks.create(data);
const task = await models.Tasks.createTask(data);

const { customerId = '' } = data;

Expand Down
6 changes: 2 additions & 4 deletions packages/plugin-cards-api/src/models/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,7 @@ export const createBoardItem = async (
models: IModels,
subdomain: string,
doc: IItemCommonFields,
type: string,
clientPortalId?: string
type: string
) => {
const { collection } = await getCollection(models, type);

Expand Down Expand Up @@ -450,8 +449,7 @@ export const createBoardItem = async (
action,
content,
createdBy: item.userId || '',
contentId: item._id,
clientPortalId
contentId: item._id
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import React from 'react';
import Tip from '@erxes/ui/src/components/Tip';
import dayjs from 'dayjs';
import { renderUserFullName } from '@erxes/ui/src/utils';
import { getCPUserName } from '@erxes/ui-log/src/activityLogs/utils';

type Props = {
activity: any;
Expand All @@ -24,14 +25,6 @@ class BoardItemCreatedLog extends React.Component<Props> {

let userName = 'Unknown';

if (createdByDetail && createdByDetail.type === 'user') {
const { content } = createdByDetail;

if (content && content.details) {
userName = renderUserFullName(createdByDetail.content);
}
}

const body = (
<Link
to={`/${contentType}/board?_id=${activity._id}&itemId=${contentDetail._id}`}
Expand All @@ -41,6 +34,27 @@ class BoardItemCreatedLog extends React.Component<Props> {
</Link>
);

if (createdByDetail && createdByDetail.type === 'user') {
const { content } = createdByDetail;

if (content && content.details) {
userName = renderUserFullName(createdByDetail.content);
}
}

if (createdByDetail && createdByDetail.type === 'clientPortalUser') {
userName = getCPUserName(createdByDetail.content);
const cpUrl = createdByDetail.content.clientPortal.url || '';
return (
<span>
<strong>{userName}</strong> created {body} {contentType} from{' '}
<a href={cpUrl} target="_blank">
{createdByDetail.content.clientPortal.name || 'client portal'}
</a>
</span>
);
}

return (
<span>
<strong>{userName}</strong> created {body} {contentType}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const cardDeleteHandler = async (models: IModels, subdomain, params) => {
card._id
);

await models.ClientPortalUserCards.remove({ cardId: card._id });
await models.ClientPortalUserCards.deleteMany({ cardId: card._id });

if (userIds.length === 0) {
return;
Expand Down
55 changes: 55 additions & 0 deletions packages/plugin-clientportal-api/src/commands/migrateUserCards.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import mongoDb from 'mongodb';
import { nanoid } from 'nanoid';

const MongoClient = mongoDb.MongoClient;

const MONGO_URL = process.argv[2] || 'mongodb://localhost:27017/erxes';

console.log('MONGO_URL', MONGO_URL);

if (!MONGO_URL) {
throw new Error(`Environment variable MONGO_URL not set.`);
}

const client = new MongoClient(MONGO_URL, { useUnifiedTopology: true });

let db;

let Cards;

const command = async () => {
try {
await client.connect();
console.log('Connected to ', MONGO_URL);
db = client.db();

Cards = db.collection('client_portal_user_cards');

const cards = await Cards.find({}).toArray();

for (const card of cards) {
for (const userId of card.userIds) {
const doc = {
_id: nanoid(),
contentType: card.type,
contentTypeId: card.cardId,
createdAt: card.createdAt,
cpUserId: userId,
};
await Cards.insertOne(doc);
}

await Cards.deleteOne({ _id: card._id });
}
}
catch (e) {
console.error("eeeeeeee ",e);
}


console.log(`Process finished at: ${new Date()}`);

process.exit();
};

command();
Original file line number Diff line number Diff line change
Expand Up @@ -88,31 +88,11 @@ const clientPortalMutations = {
isRPC: true
});

await models.ClientPortalUserCards.createOrUpdateCard(
{
type,
cardId: card._id
},
cpUser._id
);

const cp = await models.ClientPortals.findOne({
_id: cpUser.clientPortalId
}).lean();

const userName = getUserName(cpUser);

// putActivityLog(subdomain, {
// action: 'create',
// data: {
// contentType: `cards:${type}`,
// action: 'create',
// contentId: card._id,
// content: `${userName} created a cards:${type} from ${cp.url}`,
// createdBy: cpUser._id,
// clientPortalId: cp._id,
// },
// });
await models.ClientPortalUserCards.createOrUpdateCard({
contentType: type,
contentTypeId: card._id,
cpUserId: cpUser._id
});

return card;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ const clientPortalCommentMutations = {
userId: userId || user._id
});

const relatedCard = await models.ClientPortalUserCards.findOne({
cardId: typeId
});
const relatedCards = await models.ClientPortalUserCards.getUserIds(
type,
typeId
);

if (!relatedCard || userType === 'client') {
if (!relatedCards || relatedCards.length === 0 || userType === 'client') {
return comment;
}

const { userIds } = relatedCard;

for (const cardUserId of userIds) {
for (const cardUserId of relatedCards) {
await sendNotification(models, subdomain, {
receivers: [cardUserId],
title: `${user.details?.fullName} has commented on your ${type}.`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ const configClientPortalQueries = {
{ contentType, contentTypeId, userKind },
{ models }: IContext
) {
const userIds = await models.ClientPortalUserCards.find({
type: contentType,
cardId: contentTypeId
}).distinct('userIds');
const userIds = await models.ClientPortalUserCards.getUserIds(
contentType,
contentTypeId
);

if (!userIds || userIds.length === 0) {
return [];
Expand Down
97 changes: 34 additions & 63 deletions packages/plugin-clientportal-api/src/models/ClientPortalUserCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@ import {
} from './definitions/clientPortalUserCards';

export interface ICPUserCardModel extends Model<ICPUserCardDocument> {
createOrUpdateCard(
doc: ICPUserCard,
userId: string
): Promise<ICPUserCardDocument>;
removeUserFromCard(
cardId: string,
userId: string
): Promise<ICPUserCardDocument>;
getUserIds(type: string, cardId: string): Promise<string[]>;
createOrUpdateCard(doc: ICPUserCard): Promise<ICPUserCardDocument>;
getUserIds(contentType: string, contentTypeId: string): Promise<string[]>;
}

export const loadUserCardClass = (models: IModels) => {
Expand All @@ -30,58 +23,30 @@ export const loadUserCardClass = (models: IModels) => {
*/
public static async createOrUpdateCard(doc: ICPUserCard, userId: string) {
const card = await models.ClientPortalUserCards.findOne({
type: doc.type,
cardId: doc.cardId
contentType: doc.contentType,
contentTypeId: doc.contentTypeId
});

if (!card) {
return models.ClientPortalUserCards.create({
...doc,
userIds: [userId]
userId
});
}

if (card.userIds.indexOf(userId) === -1) {
card.userIds.push(userId);

await card.save();

return card;
} else {
return card;
}
}

/**
* Remove user from card
* @param cardId
* @param userId
* @return {Promise<ICPUserCardDocument>}
* @memberof CleintPortalUserCard
*/

public static async removeUserFromCard(cardId: string, userId: string) {
const card = await models.ClientPortalUserCards.findOne({
cardId
await models.ClientPortalUserCards.updateOne(
{ _id: card._id },
{
$set: {
...doc,
modifiedAt: new Date()
}
}
);

return models.ClientPortalUserCards.findOne({
_id: card._id
});

if (!card) {
throw new Error('Card not found');
}

const index = card.userIds.indexOf(userId);

if (index > -1) {
card.userIds.splice(index, 1);
}

await card.save();

if (card.userIds.length === 0) {
await card.remove();
}

return card;
}

/**
Expand All @@ -91,17 +56,23 @@ export const loadUserCardClass = (models: IModels) => {
* @memberof CleintPortalUserCard
*/

public static async getUserIds(type: string, cardId: string) {
const card = await models.ClientPortalUserCards.findOne({
cardId,
type
});

if (!card) {
throw new Error('Card not found');
}

return card.userIds;
public static async getUserIds(contentType: string, contentTypeId: string) {
// aggregate and return array of cpUserId field of ClientPortalUserCards
const userIds = await models.ClientPortalUserCards.aggregate([
{
$match: {
contentType,
contentTypeId
}
},
{
$project: {
cpUserId: 1
}
}
]);

return userIds.map((user: any) => user.cpUserId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export interface IUser {
clientPortalId: string;
erxesCustomerId?: string;
erxesCompanyId?: string;
companyIds?: string[];
createdAt?: Date;
modifiedAt?: Date;
resetPasswordToken?: string;
Expand Down Expand Up @@ -169,7 +168,6 @@ export const clientPortalUserSchema = new Schema({
label: 'Company registration number'
}),
clientPortalId: field({ type: String, required: true }),
companyIds: field({ type: [String], default: [] }),
erxesCompanyId: field({ type: String, optional: true }),
erxesCustomerId: field({ type: String, optional: true }),
phoneVerificationCode: field({ type: String, optional: true }),
Expand Down
Loading

0 comments on commit 3068c22

Please sign in to comment.