import * as lodash from '@microsoft/sp-lodash-subset' ; import { ICrmDataProvider } from './ICrmDataProvider' ; import { ISPUser } from '../data/ISPUser' ; import { IPerson } from '../data/IPerson' ; import { IOrganization } from '../data/IOrganization' ; import { ITag } from '../data/ITag' ; import { ISPView } from '../data/ISPView' ; import { IOrganizationSet } from './IOrganizationSet' ; import OrganizationSet from './OrganizationSet' ; import { ITagSet } from './ITagSet' ; import TagSet from './TagSet' ; import { BaseCrmDataProvider } from './BaseCrmDataProvider' ; import Utility from '../utilities/Utility' ; export default class MockCrmDataProvider extends BaseCrmDataProvider implements ICrmDataProvider { private _maxNumberOfPersons : number ; private _usersStore : { [ listTitle ] : ISPUser [ ] ; } ; private _personItemsStore : { [ listTitle ] : IPerson [ ] ; } ; private _organizationItemsStore : { [ listTitle ] : IOrganization [ ] ; } ; private _tagItemsStore : { [ listTitle ] : ITag [ ] ; } ; constructor ( ) { super ( ) ; this . _idCounter = 0 ; let orgs = new Array ( ) ; for ( let i = 0 ; i < 10 ; i ++ ) { orgs . push ( this . _generateOrganizationItem ( 'Contoso.' + i + '.' + Utility . generateRandomId ( ) , 'Alpha' ) ) ; } for ( let i = 0 ; i < 10 ; i ++ ) { orgs . push ( this . _generateTagItem ( 'Contoso.' + i + '.' + Utility . generateRandomId ( ) ) ) ; } orgs . push ( this . _generateOrganizationItem ( 'Fabrikam' , 'Bravo' ) ) ; orgs . push ( this . _generateOrganizationItem ( 'VanArsdel Ltd' , 'Bravo' ) ) ; let people = new Array ( ) ; for ( let i = 0 ; i < 20 ; i ++ ) { let org = null ; if ( i % 20 > 0 ) { org = orgs [ i % 40 ] ; } let orgId = org ? org . Id : null ; people . push ( this . _generatePersonItem ( 'John.' + i + '.' + Utility . generateRandomId ( ) , 'Doe.' + i + '.' + Utility . generateRandomId ( ) , 'Contoso' + ( orgId ? orgId : 'none' ) , orgId ) ) ; } let tags = new Array ( ) ; tags . push ( this . _generateTagItem ( "tagA" ) ) ; tags . push ( this . _generateTagItem ( "tagB" ) ) ; for ( let i = 0 ; i < 20 ; i ++ ) { let tag = null ; if ( i % 20 > 0 ) { tag = tags [ i % 40 ] ; } tags . push ( this . _generateTagItem ( 'tag' + i + '.' + Utility . generateRandomId ( ) ) ) ; } let users = new Array ( ) ; for ( let i = 0 ; i < 20 ; i ++ ) { users . push ( this . _generateUserItem ( 'Sarah.' + i + '.' + Utility . generateRandomId ( ) , 'Roberts.' + i + '.' + Utility . generateRandomId ( ) , 'sarah' + Utility . generateRandomId ( ) + "@contoso.com" ) ) ; } this . _organizationItemsStore = { 'Organizations' : orgs } ; this . _personItemsStore = { 'Contacts' : people } ; this . _tagItemsStore = { 'Tags' : tags } ; this . _usersStore = { 'Users' : users } ; } public updateAll ( ) { return new Promise < boolean > ( ( resolve ) => { resolve ( true ) ; } ) ; } public addPersonItem ( newPerson ) { this . _personItemsStore [ this . _selectedPersonList . Title ] = this . _personItemsStore [ this . _selectedPersonList . Title ] . concat ( newPerson ) ; this . _onPersonAdded . dispatch ( this , newPerson ) ; return new Promise < IPerson [ ] > ( ( resolve ) => { setTimeout ( ( ) => { resolve ( [ newPerson ] ) ; } , 500 ) ; } ) ; } public readPersonItems ( ) { return new Promise < IPerson [ ] > ( ( resolve ) => { setTimeout ( ( ) => resolve ( this . _personItemsStore [ this . _selectedPersonList . Title ] ) , 500 ) ; } ) ; } public readUsersByIds ( ids ) { const items = this . _usersStore [ "Users" ] . slice ( 0 , this . _maxNumberOfPersons ) ; const filteredItems = new Array ( ) ; for ( let item of items ) { if ( ids != null ) { for ( let id of ids ) { if ( id == item . Id ) { filteredItems . push ( item ) ; } } } } return new Promise < ISPUser [ ] > ( ( resolve ) => { setTimeout ( ( ) => resolve ( filteredItems ) , 500 ) ; } ) ; } public readUsersByUserName ( userName ) { const items = this . _usersStore [ "Users" ] . slice ( 0 , this . _maxNumberOfPersons ) ; const filteredItems = new Array ( ) ; for ( let item of items ) { if ( item . Name == userName || item . EMail == userName ) { filteredItems . push ( item ) ; } } return new Promise < ISPUser [ ] > ( ( resolve ) => { setTimeout ( ( ) => resolve ( filteredItems ) , 500 ) ; } ) ; } public readUsersBySearch ( search ) { const items = this . _usersStore [ "Users" ] . slice ( 0 , this . _maxNumberOfPersons ) ; const filteredItems = new Array ( ) ; search = search . toLowerCase ( ) ; for ( let item of items ) { var searchBody = item . Title + "|" + item . EMail ; searchBody = searchBody . toLowerCase ( ) ; if ( searchBody . indexOf ( search ) >= 0 ) { filteredItems . push ( item ) ; } } return new Promise < ISPUser [ ] > ( ( resolve ) => { setTimeout ( ( ) => resolve ( filteredItems ) , 500 ) ; } ) ; } public readPersonItemsBySearch ( search ) { const items = this . _personItemsStore [ this . _selectedPersonList . Title ] . slice ( 0 , this . _maxNumberOfPersons ) ; const filteredItems = new Array ( ) ; search = search . toLowerCase ( ) ; for ( let item of items ) { var searchBody = item . Title + "|" + item . FirstName + "|" + item . Company ; searchBody = searchBody . toLowerCase ( ) ; if ( searchBody . indexOf ( search ) >= 0 ) { filteredItems . push ( item ) ; } } return new Promise < IPerson [ ] > ( ( resolve ) => { setTimeout ( ( ) => resolve ( filteredItems ) , 500 ) ; } ) ; } public readPersonItemsByIds ( ids ) { const items = this . _personItemsStore [ this . _selectedPersonList . Title ] . slice ( 0 , this . _maxNumberOfPersons ) ; const filteredItems = new Array ( ) ; for ( let item of items ) { for ( var id of ids ) { if ( item . Id == id ) { filteredItems . push ( item ) ; } } } return new Promise < IPerson [ ] > ( ( resolve ) => { setTimeout ( ( ) => resolve ( filteredItems ) , 500 ) ; } ) ; } public readPersonItemsByOrganizationId ( organizationId ) { const items = this . _personItemsStore [ this . _selectedPersonList . Title ] . slice ( 0 , this . _maxNumberOfPersons ) ; const filteredItems = new Array ( ) ; for ( let item of items ) { if ( item . OrganizationId == organizationId ) { filteredItems . push ( item ) ; } } return new Promise < IPerson [ ] > ( ( resolve ) => { setTimeout ( ( ) => resolve ( filteredItems ) , 500 ) ; } ) ; } public updatePersonItem ( itemUpdated ) { const index = lodash . findIndex ( this . _personItemsStore [ this . _selectedPersonList . Title ] , ( item ) => item . Id === itemUpdated . Id ) ; if ( index !== - 1 ) { this . _personItemsStore [ this . _selectedPersonList . Title ] [ index ] = itemUpdated ; return this . readPersonItems ( ) ; } else { return Promise . reject ( new Error ( `Item to update doesn't exist.` ) ) ; } } public deletePersonItem ( itemDeleted ) { this . _personItemsStore [ this . _selectedPersonList . Title ] = this . _personItemsStore [ this . _selectedPersonList . Title ] . filter ( ( item ) => item . Id !== itemDeleted . Id ) ; return this . readPersonItems ( ) ; } private _generatePersonItem ( firstName , lastName , company , organizationId ? ) { return { Id : this . _idCounter -- , Company : company , FirstName : firstName , Title : lastName , OrganizationId : organizationId , AuthorId : 1 , Created : new Date ( ) , Modified : new Date ( ) , EditorId : 1 } ; } private _generateUserItem ( firstName , lastName , email ) { return { Id : this . _idCounter -- , EMail : email , FirstName : firstName , LastName : lastName , Title : firstName + " " + lastName , AuthorId : 1 , Created : new Date ( ) , Modified : new Date ( ) , EditorId : 1 } ; } public addOrganizationItem ( newOrganization ) { this . _organizationItemsStore [ this . _selectedOrganizationList . Title ] = this . _organizationItemsStore [ this . _selectedOrganizationList . Title ] . concat ( newOrganization ) ; this . _onOrganizationAdded . dispatch ( this , newOrganization ) ; let ose = this . _ensureOrganizationSet ( "NEW" + newOrganization . Id ) ; ose . apply ( [ newOrganization ] , true ) ; return new Promise < IOrganizationSet > ( ( resolve ) => { setTimeout ( ( ) => resolve ( ose ) , 500 ) ; } ) ; } public init ( ) { return this . _loadListData ( ) ; } public _loadListData ( ) { return new Promise < void > ( ( resolve ) => { setTimeout ( ( ) => resolve ( ) , 500 ) ; } ) ; } public readOrganizationItems ( ) { const items = this . _organizationItemsStore [ this . _selectedOrganizationList . Title ] . slice ( 0 , this . _maxNumberOfPersons ) ; return new Promise < IOrganizationSet > ( ( resolve ) => { let orgSet = this . _ensureOrganizationSet ( "" ) ; this . notifyOrganizationsChanged ( items , orgSet ) ; setTimeout ( ( ) => resolve ( orgSet ) , 500 ) ; } ) ; } public readRecentOrganizationItems ( ) { return this . readOrganizationItems ( ) ; } public readMyOrganizationItems ( ) { return this . readOrganizationItems ( ) ; } public readOrganizationItemsByIds ( ids ) { const items = this . _organizationItemsStore [ this . _selectedOrganizationList . Title ] . slice ( 0 , this . _maxNumberOfPersons ) ; const filteredItems = new Array ( ) ; for ( let item of items ) { for ( var id of ids ) { if ( item . Id == id ) { filteredItems . push ( item ) ; } } } return new Promise < IOrganizationSet > ( ( resolve ) => { var sId = "" ; for ( var idA of ids ) { sId += idA + "." ; } let orgSet = this . _ensureOrganizationSet ( sId ) ; this . notifyOrganizationsChanged ( filteredItems , orgSet ) ; setTimeout ( ( ) => resolve ( orgSet ) , 500 ) ; } ) ; } public readOrganizationItemsByView ( view ) { const items = this . _organizationItemsStore [ this . _selectedOrganizationList . Title ] . slice ( 0 , this . _maxNumberOfPersons ) ; const filteredItems = new Array ( ) ; for ( let item of items ) { if ( view . query != null && view . query . matches ( item ) ) { filteredItems . push ( item ) ; } } return new Promise < IOrganizationSet > ( ( resolve ) => { let orgSet = this . _ensureOrganizationSet ( view . query . getOdataQuery ( this . selectedOrganizationList ) ) ; this . notifyOrganizationsChanged ( filteredItems , orgSet ) ; setTimeout ( ( ) => resolve ( orgSet ) , 500 ) ; } ) ; } public readOrganizationItemsByPriority ( chosenPriority ) { const items = this . _organizationItemsStore [ this . _selectedOrganizationList . Title ] . slice ( 0 , this . _maxNumberOfPersons ) ; const filteredItems = new Array ( ) ; for ( let item of items ) { var itemPri = item [ "Organizational_x0020_Priority" ] ; if ( chosenPriority >= 30 && ( itemPri == null || itemPri > 32 ) ) { filteredItems . push ( item ) ; } else if ( itemPri == chosenPriority ) { filteredItems . push ( item ) ; } } return new Promise < IOrganizationSet > ( ( resolve ) => { let orgSet = this . _ensureOrganizationSet ( "PRIO" + chosenPriority ) ; this . notifyOrganizationsChanged ( filteredItems , orgSet ) ; setTimeout ( ( ) => resolve ( orgSet ) , 500 ) ; } ) ; } public readOrganizationItemsBySearch ( search , tagItems ) { const items = this . _organizationItemsStore [ this . _selectedOrganizationList . Title ] . slice ( 0 , this . _maxNumberOfPersons ) ; const filteredItems = new Array ( ) ; if ( search != null && search . length > 3 ) { search = search . toLowerCase ( ) ; for ( let item of items ) { var searchBody = item . Title ; searchBody = searchBody . toLowerCase ( ) ; if ( searchBody . indexOf ( search ) >= 0 ) { filteredItems . push ( item ) ; } } } if ( tagItems != null && tagItems . length >= 0 ) { for ( let item of items ) { var tags = item [ "TagsId" ] ; if ( tags != null && tags . length != null && tags . length > 0 ) { for ( var itemTagId of tags ) { for ( var searchTagItemId of tagItems ) { if ( itemTagId == searchTagItemId ) { filteredItems . push ( item ) ; } } } } } } return new Promise < IOrganizationSet > ( ( resolve ) => { let orgSet = this . _ensureOrganizationSet ( "SEARCH" + search ) ; this . notifyOrganizationsChanged ( filteredItems , orgSet ) ; setTimeout ( ( ) => resolve ( orgSet ) , 500 ) ; } ) ; } public updateOrganizationItem ( itemUpdated ) { const index = lodash . findIndex ( this . _organizationItemsStore [ this . _selectedOrganizationList . Title ] , ( item ) => item . Id === itemUpdated . Id ) ; if ( index !== - 1 ) { this . _organizationItemsStore [ this . _selectedOrganizationList . Title ] [ index ] = itemUpdated ; return this . readOrganizationItems ( ) ; } else { return Promise . reject ( new Error ( `Item to update doesn't exist.` ) ) ; } } public deleteOrganizationItem ( itemDeleted ) { this . _organizationItemsStore [ this . _selectedOrganizationList . Title ] = this . _organizationItemsStore [ this . _selectedOrganizationList . Title ] . filter ( ( item ) => item . Id !== itemDeleted . Id ) ; return this . readOrganizationItems ( ) ; } private _generateOrganizationItem ( title , type ) { return { Id : this . _idCounter -- , Title : title , AuthorId : 1 , Created : new Date ( ) , Modified : new Date ( ) , EditorId : 1 } ; } private _generateTagItem ( title ) { return { Id : this . _idCounter -- , Title : title , AuthorId : 1 , Created : new Date ( ) , Modified : new Date ( ) , EditorId : 1 } ; } public addTagItem ( newTag ) { this . _tagItemsStore [ this . _selectedTagList . Title ] = this . _tagItemsStore [ this . _selectedTagList . Title ] . concat ( newTag ) ; this . _onTagAdded . dispatch ( this , newTag ) ; let ose = this . _ensureTagSet ( "NEW" + newTag . Id ) ; ose . apply ( [ newTag ] , true ) ; return new Promise < ITagSet > ( ( resolve ) => { setTimeout ( ( ) => resolve ( ose ) , 500 ) ; } ) ; } public readTagItems ( ) { const items = this . _tagItemsStore [ this . _selectedTagList . Title ] . slice ( 0 , this . _maxNumberOfPersons ) ; return new Promise < ITagSet > ( ( resolve ) => { let orgSet = this . _ensureTagSet ( "" ) ; this . notifyTagsChanged ( items , orgSet ) ; setTimeout ( ( ) => resolve ( orgSet ) , 500 ) ; } ) ; } public readRecentTagItems ( ) { return this . readTagItems ( ) ; } public readMyTagItems ( ) { return this . readTagItems ( ) ; } public readTagItemsByIds ( ids ) { const items = this . _tagItemsStore [ this . _selectedTagList . Title ] . slice ( 0 , this . _maxNumberOfPersons ) ; const filteredItems = new Array ( ) ; for ( let item of items ) { for ( var id of ids ) { if ( item . Id == id ) { filteredItems . push ( item ) ; } } } return new Promise < ITagSet > ( ( resolve ) => { let tagSet = this . _ensureTagSet ( id + "" ) ; this . notifyTagsChanged ( filteredItems , tagSet ) ; setTimeout ( ( ) => resolve ( tagSet ) , 500 ) ; } ) ; } public readTagItemsBySearch ( search ) { const items = this . _tagItemsStore [ this . _selectedTagList . Title ] . slice ( 0 , this . _maxNumberOfPersons ) ; const filteredItems = new Array ( ) ; search = search . toLowerCase ( ) ; for ( let item of items ) { var searchBody = item . Title ; searchBody = searchBody . toLowerCase ( ) ; if ( searchBody . indexOf ( search ) >= 0 ) { filteredItems . push ( item ) ; } } return new Promise < ITagSet > ( ( resolve ) => { let orgSet = this . _ensureTagSet ( "SEARCH" + search ) ; this . notifyTagsChanged ( filteredItems , orgSet ) ; setTimeout ( ( ) => resolve ( orgSet ) , 500 ) ; } ) ; } public updateTagItem ( itemUpdated ) { const index = lodash . findIndex ( this . _tagItemsStore [ this . _selectedTagList . Title ] , ( item ) => item . Id === itemUpdated . Id ) ; if ( index !== - 1 ) { this . _tagItemsStore [ this . _selectedTagList . Title ] [ index ] = itemUpdated ; return this . readTagItems ( ) ; } else { return Promise . reject ( new Error ( `Item to update doesn't exist.` ) ) ; } } public deleteTagItem ( itemDeleted ) { this . _tagItemsStore [ this . _selectedTagList . Title ] = this . _tagItemsStore [ this . _selectedTagList . Title ] . filter ( ( item ) => item . Id !== itemDeleted . Id ) ; return this . readTagItems ( ) ; } }