1616import { type Attachment } from '@hcengineering/attachment'
1717import contact , { Employee , type Person } from '@hcengineering/contact'
1818import {
19+ buildSocialIdString ,
1920 type Class ,
2021 type Doc ,
2122 generateId ,
2223 PersonId ,
2324 type Ref ,
25+ SocialIdType ,
2426 type Space ,
2527 type TxOperations
2628} from '@hcengineering/core'
@@ -330,24 +332,26 @@ interface AttachmentMetadata {
330332}
331333
332334export class UnifiedFormatImporter {
335+ private readonly importerEmailPlaceholder = '[email protected] ' 336+ private readonly importerNamePlaceholder = 'New User'
333337 private readonly pathById = new Map < Ref < Doc > , string > ( )
334338 private readonly refMetaByPath = new Map < string , ReferenceMetadata > ( )
335339 private readonly fileMetaByPath = new Map < string , AttachmentMetadata > ( )
336340 private readonly ctrlDocTemplateIdByPath = new Map < string , Ref < ControlledDocument > > ( )
337341
338342 private personsByName = new Map < string , Ref < Person > > ( )
339- // private accountsByEmail = new Map<string, Ref<PersonAccount>>()
340- // private employeesByName = new Map<string, Ref<Employee>>()
343+ private employeesByName = new Map < string , Ref < Employee > > ( )
341344
342345 constructor (
343346 private readonly client : TxOperations ,
344347 private readonly fileUploader : FileUploader ,
345- private readonly logger : Logger
348+ private readonly logger : Logger ,
349+ private readonly importerSocialId ?: PersonId ,
350+ private readonly importerPerson ?: Ref < Person >
346351 ) { }
347352
348353 private async initCaches ( ) : Promise < void > {
349354 await this . cachePersonsByNames ( )
350- await this . cacheAccountsByEmails ( )
351355 await this . cacheEmployeesByName ( )
352356 }
353357
@@ -588,31 +592,31 @@ export class UnifiedFormatImporter {
588592 if ( name === undefined ) {
589593 return undefined
590594 }
595+
596+ if ( name === this . importerNamePlaceholder && this . importerPerson != null ) {
597+ return this . importerPerson
598+ }
591599 const person = this . personsByName . get ( name )
592600 if ( person === undefined ) {
593601 throw new Error ( `Person not found: ${ name } ` )
594602 }
595603 return person
596604 }
597605
598- private findAccountByEmail ( email : string ) : PersonId {
599- // TODO: FIXME
600- throw new Error ( 'Not implemented' )
601- // const account = this.accountsByEmail.get(email)
602- // if (account === undefined) {
603- // throw new Error(`Account not found: ${email}`)
604- // }
605- // return account
606+ private getSocialIdByEmail ( email : string ) : PersonId {
607+ if ( email === this . importerEmailPlaceholder && this . importerSocialId != null ) {
608+ return this . importerSocialId
609+ }
610+
611+ return buildSocialIdString ( { type : SocialIdType . EMAIL , value : email } )
606612 }
607613
608614 private findEmployeeByName ( name : string ) : Ref < Employee > {
609- // TODO: FIXME
610- throw new Error ( 'Not implemented' )
611- // const employee = this.employeesByName.get(name)
612- // if (employee === undefined) {
613- // throw new Error(`Employee not found: ${name}`)
614- // }
615- // return employee
615+ const employee = this . employeesByName . get ( name )
616+ if ( employee === undefined ) {
617+ throw new Error ( `Employee not found: ${ name } ` )
618+ }
619+ return employee
616620 }
617621
618622 private async processDocumentsRecursively (
@@ -752,7 +756,7 @@ export class UnifiedFormatImporter {
752756 }
753757 return {
754758 text : comment . text ,
755- author : this . findAccountByEmail ( comment . author ) ,
759+ author : this . getSocialIdByEmail ( comment . author ) ,
756760 attachments
757761 }
758762 } )
@@ -789,9 +793,9 @@ export class UnifiedFormatImporter {
789793 defaultIssueStatus :
790794 projectHeader . defaultIssueStatus !== undefined ? { name : projectHeader . defaultIssueStatus } : undefined ,
791795 owners :
792- projectHeader . owners !== undefined ? projectHeader . owners . map ( ( email ) => this . findAccountByEmail ( email ) ) : [ ] ,
796+ projectHeader . owners !== undefined ? projectHeader . owners . map ( ( email ) => this . getSocialIdByEmail ( email ) ) : [ ] ,
793797 members :
794- projectHeader . members !== undefined ? projectHeader . members . map ( ( email ) => this . findAccountByEmail ( email ) ) : [ ] ,
798+ projectHeader . members !== undefined ? projectHeader . members . map ( ( email ) => this . getSocialIdByEmail ( email ) ) : [ ] ,
795799 docs : [ ]
796800 }
797801 }
@@ -805,9 +809,9 @@ export class UnifiedFormatImporter {
805809 archived : spaceHeader . archived ?? false ,
806810 description : spaceHeader . description ,
807811 emoji : spaceHeader . emoji ,
808- owners : spaceHeader . owners !== undefined ? spaceHeader . owners . map ( ( email ) => this . findAccountByEmail ( email ) ) : [ ] ,
812+ owners : spaceHeader . owners !== undefined ? spaceHeader . owners . map ( ( email ) => this . getSocialIdByEmail ( email ) ) : [ ] ,
809813 members :
810- spaceHeader . members !== undefined ? spaceHeader . members . map ( ( email ) => this . findAccountByEmail ( email ) ) : [ ] ,
814+ spaceHeader . members !== undefined ? spaceHeader . members . map ( ( email ) => this . getSocialIdByEmail ( email ) ) : [ ] ,
811815 docs : [ ]
812816 }
813817 }
@@ -819,11 +823,11 @@ export class UnifiedFormatImporter {
819823 private : spaceHeader . private ?? false ,
820824 archived : spaceHeader . archived ?? false ,
821825 description : spaceHeader . description ,
822- owners : spaceHeader . owners ?. map ( ( email ) => this . findAccountByEmail ( email ) ) ?? [ ] ,
823- members : spaceHeader . members ?. map ( ( email ) => this . findAccountByEmail ( email ) ) ?? [ ] ,
824- qualified : spaceHeader . qualified !== undefined ? this . findAccountByEmail ( spaceHeader . qualified ) : undefined ,
825- manager : spaceHeader . manager !== undefined ? this . findAccountByEmail ( spaceHeader . manager ) : undefined ,
826- qara : spaceHeader . qara !== undefined ? this . findAccountByEmail ( spaceHeader . qara ) : undefined ,
826+ owners : spaceHeader . owners ?. map ( ( email ) => this . getSocialIdByEmail ( email ) ) ?? [ ] ,
827+ members : spaceHeader . members ?. map ( ( email ) => this . getSocialIdByEmail ( email ) ) ?? [ ] ,
828+ qualified : spaceHeader . qualified !== undefined ? this . getSocialIdByEmail ( spaceHeader . qualified ) : undefined ,
829+ manager : spaceHeader . manager !== undefined ? this . getSocialIdByEmail ( spaceHeader . manager ) : undefined ,
830+ qara : spaceHeader . qara !== undefined ? this . getSocialIdByEmail ( spaceHeader . qara ) : undefined ,
827831 docs : [ ]
828832 }
829833 }
@@ -950,30 +954,18 @@ export class UnifiedFormatImporter {
950954 } , new Map ( ) )
951955 }
952956
953- private async cacheAccountsByEmails ( ) : Promise < void > {
954- // TODO: FIXME
955- throw new Error ( 'Not implemented' )
956- // const accounts = await this.client.findAll(contact.class.PersonAccount, {})
957- // this.accountsByEmail = accounts.reduce((map, account) => {
958- // map.set(account.email, account._id)
959- // return map
960- // }, new Map())
961- }
962-
963957 private async cacheEmployeesByName ( ) : Promise < void > {
964- // TODO: FIXME
965- throw new Error ( 'Not implemented' )
966- // this.employeesByName = (await this.client.findAll(contact.mixin.Employee, {}))
967- // .map((employee) => {
968- // return {
969- // _id: employee._id,
970- // name: employee.name.split(',').reverse().join(' ')
971- // }
972- // })
973- // .reduce((refByName, employee) => {
974- // refByName.set(employee.name, employee._id)
975- // return refByName
976- // }, new Map())
958+ this . employeesByName = ( await this . client . findAll ( contact . mixin . Employee , { } ) )
959+ . map ( ( employee ) => {
960+ return {
961+ _id : employee . _id ,
962+ name : employee . name . split ( ',' ) . reverse ( ) . join ( ' ' )
963+ }
964+ } )
965+ . reduce ( ( refByName , employee ) => {
966+ refByName . set ( employee . name , employee . _id )
967+ return refByName
968+ } , new Map ( ) )
977969 }
978970
979971 private async collectFileMetadata ( folderPath : string ) : Promise < void > {
0 commit comments