@hed-hog/contact 0.0.312 → 0.0.314

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -786,8 +786,12 @@ export class PersonService {
786
786
  };
787
787
  }
788
788
 
789
- async getOwnerOptions(currentUserId?: number) {
790
- const where: Prisma.userWhereInput = {
789
+ async getOwnerOptions(currentUserId?: number, search?: string) {
790
+ const searchFilter: Prisma.userWhereInput | undefined = search
791
+ ? { name: { contains: search, mode: 'insensitive' } }
792
+ : undefined;
793
+
794
+ const roleFilter: Prisma.userWhereInput = {
791
795
  OR: [
792
796
  {
793
797
  role_user: {
@@ -810,11 +814,20 @@ export class PersonService {
810
814
  ],
811
815
  };
812
816
 
817
+ const where: Prisma.userWhereInput = searchFilter
818
+ ? { AND: [roleFilter, searchFilter] }
819
+ : roleFilter;
820
+
813
821
  const users = await this.prismaService.user.findMany({
814
822
  where,
815
823
  select: {
816
824
  id: true,
817
825
  name: true,
826
+ user_identifier: {
827
+ where: { type: 'email' },
828
+ select: { value: true },
829
+ take: 1,
830
+ },
818
831
  },
819
832
  orderBy: {
820
833
  name: 'asc',
@@ -822,18 +835,46 @@ export class PersonService {
822
835
  take: 500,
823
836
  });
824
837
 
825
- const byId = new Map<number, { id: number; name: string }>();
838
+ const byId = new Map<number, { id: number; name: string; email: string }>();
826
839
  for (const user of users) {
827
840
  if (!user?.id) continue;
828
841
  byId.set(user.id, {
829
842
  id: user.id,
830
843
  name: user.name || `#${user.id}`,
844
+ email: (user.user_identifier[0]?.value) || '',
831
845
  });
832
846
  }
833
847
 
834
848
  return Array.from(byId.values());
835
849
  }
836
850
 
851
+ async getLinkedUserOptions(search?: string) {
852
+ const where: Prisma.userWhereInput = search
853
+ ? { name: { contains: search, mode: 'insensitive' } }
854
+ : {};
855
+
856
+ const users = await this.prismaService.user.findMany({
857
+ where,
858
+ select: {
859
+ id: true,
860
+ name: true,
861
+ user_identifier: {
862
+ where: { type: 'email' },
863
+ select: { value: true },
864
+ take: 1,
865
+ },
866
+ },
867
+ orderBy: { name: 'asc' },
868
+ take: 500,
869
+ });
870
+
871
+ return users.map((user) => ({
872
+ id: user.id,
873
+ name: user.name || `#${user.id}`,
874
+ email: user.user_identifier[0]?.value || '',
875
+ }));
876
+ }
877
+
837
878
  async checkDuplicates(query: CheckPersonDuplicatesQueryDTO) {
838
879
  const excludedPersonId = this.coerceNumber(query.person_id);
839
880
  const normalizedEmail = this.normalizeEmail(query.email);
@@ -1184,6 +1225,16 @@ export class PersonService {
1184
1225
  contact: true,
1185
1226
  document: true,
1186
1227
  person_metadata: true,
1228
+ person_user: {
1229
+ include: {
1230
+ user: {
1231
+ select: {
1232
+ id: true,
1233
+ name: true,
1234
+ },
1235
+ },
1236
+ },
1237
+ },
1187
1238
  },
1188
1239
  },
1189
1240
  );
@@ -1917,6 +1968,21 @@ export class PersonService {
1917
1968
  contact: true,
1918
1969
  document: true,
1919
1970
  person_metadata: true,
1971
+ person_user: {
1972
+ include: {
1973
+ user: {
1974
+ select: {
1975
+ id: true,
1976
+ name: true,
1977
+ user_identifier: {
1978
+ where: { type: 'email' },
1979
+ select: { value: true },
1980
+ take: 1,
1981
+ },
1982
+ },
1983
+ },
1984
+ },
1985
+ },
1920
1986
  },
1921
1987
  });
1922
1988
 
@@ -2133,6 +2199,7 @@ export class PersonService {
2133
2199
  deal_value: data.deal_value,
2134
2200
  tags: data.tags,
2135
2201
  });
2202
+ await this.syncPersonUser(tx, person.id, data.user_id);
2136
2203
 
2137
2204
  return person;
2138
2205
  });
@@ -2219,6 +2286,7 @@ export class PersonService {
2219
2286
  await this.syncContacts(tx, id, incomingContacts);
2220
2287
  await this.syncAddresses(tx, id, incomingAddresses, locale);
2221
2288
  await this.syncDocuments(tx, id, incomingDocuments);
2289
+ await this.syncPersonUser(tx, id, data.user_id);
2222
2290
 
2223
2291
  return { success: true };
2224
2292
  })
@@ -3000,6 +3068,7 @@ export class PersonService {
3000
3068
  .map((item: any) => item.address)
3001
3069
  .filter((address: any) => address != null)
3002
3070
  : [],
3071
+ person_user: person.person_user || [],
3003
3072
  };
3004
3073
  });
3005
3074
  }
@@ -3648,6 +3717,16 @@ export class PersonService {
3648
3717
  }
3649
3718
  }
3650
3719
 
3720
+ private async syncPersonUser(tx: any, personId: number, userId: number | null | undefined) {
3721
+ if (userId === undefined) {
3722
+ return;
3723
+ }
3724
+ await tx.person_user.deleteMany({ where: { person_id: personId } });
3725
+ if (userId !== null) {
3726
+ await tx.person_user.create({ data: { person_id: personId, user_id: userId } });
3727
+ }
3728
+ }
3729
+
3651
3730
  private async syncContacts(tx: any, personId: number, incomingContacts: any[]) {
3652
3731
  const existingContacts = await tx.contact.findMany({ where: { person_id: personId } });
3653
3732