diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts index 8a802e9059..e19f1f8220 100644 --- a/packages/query/src/index.ts +++ b/packages/query/src/index.ts @@ -277,7 +277,7 @@ export class LiveQuery extends TxProcessor implements Client { // Mixin potentially added to object we doesn't have in out results const doc = await this.findOne(q._class, { _id: tx.objectId }, q.options) if (doc !== undefined) { - await this.handleDocAdd(q, doc) + await this.handleDocAdd(q, doc, false) } } } @@ -547,13 +547,13 @@ export class LiveQuery extends TxProcessor implements Client { return {} } - private async handleDocAdd (q: Query, doc: Doc): Promise { + private async handleDocAdd (q: Query, doc: Doc, handleLookup = true): Promise { if (this.match(q, doc)) { if (q.result instanceof Promise) { q.result = await q.result } - if (q.options?.lookup !== undefined) { + if (q.options?.lookup !== undefined && handleLookup) { await this.lookup(q._class, doc, q.options.lookup) } // We could already have document inside results, if query is created during processing of document create transaction and not yet handled on client. diff --git a/plugins/contact-assets/lang/en.json b/plugins/contact-assets/lang/en.json index 9314f8a72f..9a9727529f 100644 --- a/plugins/contact-assets/lang/en.json +++ b/plugins/contact-assets/lang/en.json @@ -47,7 +47,7 @@ "Homepage": "Home page", "SocialLinks": "Socail links", "ViewActivity": "View activity", - "PersonAlreadyExists": "Person already exists...", + "PersonAlreadyExists": "Contact already exists...", "Status": "Status", "SetStatus": "Set status", "ClearStatus": "Clear status", diff --git a/plugins/contact-resources/src/components/CreateOrganization.svelte b/plugins/contact-resources/src/components/CreateOrganization.svelte index f71606b0e6..c20aa08c07 100644 --- a/plugins/contact-resources/src/components/CreateOrganization.svelte +++ b/plugins/contact-resources/src/components/CreateOrganization.svelte @@ -13,14 +13,15 @@ // limitations under the License. --> @@ -83,6 +91,22 @@ /> - + it.provider)} + /> + + + {#if matches.length > 0} +
+ + + +
+
+ {/if}
diff --git a/plugins/contact/src/index.ts b/plugins/contact/src/index.ts index df4662c362..50bbc611ac 100644 --- a/plugins/contact/src/index.ts +++ b/plugins/contact/src/index.ts @@ -226,13 +226,14 @@ export default contactPlugin /** * @public */ -export async function findPerson ( +export async function findContacts ( client: Client, - person: Data, + _class: Ref>, + person: Data, channels: AttachedData[] -): Promise { - if (channels.length === 0 || person.name.length === 0) { - return [] +): Promise<{ contacts: Contact[], channels: AttachedData[] }> { + if (channels.length === 0 && person.name.length === 0) { + return { contacts: [], channels: [] } } // Take only first part of first name for match. const values = channels.map((it) => it.value) @@ -240,23 +241,33 @@ export async function findPerson ( // Same name persons const potentialChannels = await client.findAll(contactPlugin.class.Channel, { value: { $in: values } }) - let potentialPersonIds = Array.from(new Set(potentialChannels.map((it) => it.attachedTo as Ref)).values()) + let potentialContactIds = Array.from(new Set(potentialChannels.map((it) => it.attachedTo as Ref)).values()) - if (potentialPersonIds.length === 0) { - const firstName = getFirstName(person.name).split(' ').shift() ?? '' - const lastName = getLastName(person.name) - // try match using just first/last name - potentialPersonIds = ( - await client.findAll(contactPlugin.class.Person, { name: { $like: `${lastName}%${firstName}%` } }) - ).map((it) => it._id) - if (potentialPersonIds.length === 0) { - return [] + if (potentialContactIds.length === 0) { + if (client.getHierarchy().isDerived(_class, contactPlugin.class.Person)) { + const firstName = getFirstName(person.name).split(' ').shift() ?? '' + const lastName = getLastName(person.name) + // try match using just first/last name + potentialContactIds = ( + await client.findAll(contactPlugin.class.Contact, { name: { $like: `${lastName}%${firstName}%` } }) + ).map((it) => it._id) + if (potentialContactIds.length === 0) { + return { contacts: [], channels: [] } + } + } else if (client.getHierarchy().isDerived(_class, contactPlugin.class.Organization)) { + // try match using just first/last name + potentialContactIds = ( + await client.findAll(contactPlugin.class.Contact, { name: { $like: `${person.name}` } }) + ).map((it) => it._id) + if (potentialContactIds.length === 0) { + return { contacts: [], channels: [] } + } } } - const potentialPersons: FindResult = await client.findAll( - contactPlugin.class.Person, - { _id: { $in: potentialPersonIds } }, + const potentialPersons: FindResult = await client.findAll( + contactPlugin.class.Contact, + { _id: { $in: potentialContactIds } }, { lookup: { _id: { @@ -266,29 +277,40 @@ export async function findPerson ( } ) - const result: Person[] = [] - + const result: Contact[] = [] + const resChannels: AttachedData[] = [] for (const c of potentialPersons) { let matches = 0 if (c.name === person.name) { matches++ } - if (c.city === person.city) { - matches++ - } for (const ch of (c.$lookup?.channels as Channel[]) ?? []) { for (const chc of channels) { if (chc.provider === ch.provider && chc.value === ch.value.trim()) { // We have matched value + resChannels.push(chc) matches += 2 break } } } - if (matches >= 2) { + if (matches > 0) { result.push(c) } } - return result + return { contacts: result, channels: resChannels } +} + +/** + * @public + + */ +export async function findPerson ( + client: Client, + person: Data, + channels: AttachedData[] +): Promise { + const result = await findContacts(client, contactPlugin.class.Person, person, channels) + return result.contacts as Person[] } diff --git a/plugins/lead-resources/src/components/CreateCustomer.svelte b/plugins/lead-resources/src/components/CreateCustomer.svelte index 7259856dc9..338846fc93 100644 --- a/plugins/lead-resources/src/components/CreateCustomer.svelte +++ b/plugins/lead-resources/src/components/CreateCustomer.svelte @@ -14,24 +14,24 @@ --> @@ -245,7 +250,12 @@ {/if} - + it.provider)} + /> {#if matches.length > 0} diff --git a/plugins/recruit-resources/src/components/CreateCandidate.svelte b/plugins/recruit-resources/src/components/CreateCandidate.svelte index 767c319759..bd0680752a 100644 --- a/plugins/recruit-resources/src/components/CreateCandidate.svelte +++ b/plugins/recruit-resources/src/components/CreateCandidate.svelte @@ -14,7 +14,7 @@ -->