import { dbManager } from '../database/connection.js';
import { Platform } from '../types/index.js';
export const masterUserTools = [
{
name: 'find-master-user-by-platform-id',
description: 'Find master user by platform ID (case-insensitive search in PlatformUserLinks.PlatformId)',
inputSchema: {
type: 'object',
properties: {
platformId: {
type: 'string',
description: 'Platform ID to search for (searches PlatformUserLinks.PlatformId field)'
},
limit: {
type: 'number',
description: 'Maximum number of results to return',
default: 10
}
},
required: ['platformId']
}
},
{
name: 'find-master-user-by-email',
description: 'Find master user by email (case-insensitive search in PlatformUserLinks.OriginalEmail)',
inputSchema: {
type: 'object',
properties: {
email: {
type: 'string',
description: 'Email to search for'
},
limit: {
type: 'number',
description: 'Maximum number of results to return',
default: 10
}
},
required: ['email']
}
},
{
name: 'find-master-user-by-id',
description: 'Find a specific master user by their _id',
inputSchema: {
type: 'object',
properties: {
id: {
type: 'string',
description: 'Master user ID to search for'
}
},
required: ['id']
}
},
{
name: 'find-master-user-by-account-id',
description: 'Find master user by account ID (case-insensitive regex search in OwnedAccounts.AccountId)',
inputSchema: {
type: 'object',
properties: {
accountId: {
type: 'string',
description: 'Account ID to search for (searches OwnedAccounts.AccountId field)'
},
limit: {
type: 'number',
description: 'Maximum number of results to return',
default: 10
}
},
required: ['accountId']
}
}
];
export async function findMasterUserByPlatformId(platformId, limit = 10) {
await dbManager.ensureConnected();
const collection = dbManager.getMasterUserCollection();
const filter = {
'PlatformUserLinks.PlatformId': {
$regex: platformId,
$options: 'i'
}
};
const users = await collection.find(filter).limit(limit).toArray();
return users;
}
export async function findMasterUserByEmail(email, limit = 10) {
await dbManager.ensureConnected();
const collection = dbManager.getMasterUserCollection();
const filter = {
'PlatformUserLinks.OriginalEmail': {
$regex: email,
$options: 'i'
}
};
const users = await collection.find(filter).limit(limit).toArray();
return users;
}
export async function findMasterUserById(id) {
await dbManager.ensureConnected();
const collection = dbManager.getMasterUserCollection();
const user = await collection.findOne({ _id: id });
return user;
}
export async function getMasterUserPlatforms(userId) {
await dbManager.ensureConnected();
const collection = dbManager.getMasterUserCollection();
const user = await collection.findOne({ _id: userId });
return user;
}
export async function createMasterUser(data) {
await dbManager.ensureConnected();
const collection = dbManager.getMasterUserCollection();
const now = new Date();
const userId = crypto.randomUUID();
const newUser = {
_id: userId,
Id: userId,
PlatformUserLinks: [{
PlatformId: data.platformId,
Platform: data.platform,
Product: data.product,
IsFirstLink: data.isFirstLink ?? true,
OriginalEmail: data.originalEmail || null,
CreatedAt: now
}],
OwnedAccounts: [],
CreatedAt: now,
UpdatedAt: now,
DeletedAt: null
};
await collection.insertOne(newUser);
return newUser;
}
export async function findMasterUserByAccountId(accountId, limit = 10) {
await dbManager.ensureConnected();
const collection = dbManager.getMasterUserCollection();
const filter = {
'OwnedAccounts.AccountId': {
$regex: accountId,
$options: 'i'
}
};
const users = await collection.find(filter).limit(limit).toArray();
return users;
}
export async function searchMasterUsers(query, limit = 10) {
await dbManager.ensureConnected();
const collection = dbManager.getMasterUserCollection();
const filter = {
$or: [
{
'PlatformUserLinks.PlatformId': {
$regex: query,
$options: 'i'
}
},
{
'PlatformUserLinks.OriginalEmail': {
$regex: query,
$options: 'i'
}
}
]
};
const users = await collection.find(filter).limit(limit).toArray();
return users;
}