import { WebPartContext } from "@microsoft/sp-webpart-base" ; import { sp } from "@pnp/sp" ; import "@pnp/sp/webs" ; import "@pnp/sp/lists" ; import siteconfig from "../config/siteconfig.json" ; import * as stringsConstants from "../constants/strings" ; interface ICommonServicesState { } let rootSiteURL ; export default class CommonServices { constructor ( context , siteUrl ) { if ( context . pageContext . web . serverRelativeUrl == "/" ) rootSiteURL = context . pageContext . web . absoluteUrl ; else rootSiteURL = siteUrl ; rootSiteURL = rootSiteURL + "/" + siteconfig . inclusionPath + "/" + siteconfig . sitename ; sp . setup ( { sp : { baseUrl : rootSiteURL } , } ) ; } public async getItemsWithOnlyFilter ( listname , filterparametres ) { var items = [ ] ; items = await sp . web . lists . getByTitle ( listname ) . items . filter ( filterparametres ) . getAll ( ) ; return items ; } public async getAllListItems ( listname ) { var items = [ ] ; items = await sp . web . lists . getByTitle ( listname ) . items . getAll ( ) ; return items ; } public async createListItem ( listname , data ) { return sp . web . lists . getByTitle ( listname ) . items . add ( data ) ; } public updateListItem ( listName , data , id ) { return sp . web . lists . getByTitle ( listName ) . items . getById ( parseInt ( id ) ) . update ( data ) . then ( i => { return true ; } ) ; } public async createListFields ( listname , fieldsToCreate ) { return new Promise < any > ( async ( resolve , reject ) => { try { const listContext = await sp . web . lists . getByTitle ( listname ) ; const batch = sp . web . createBatch ( ) ; for ( let i = 0 ; i < fieldsToCreate . length ; i ++ ) { listContext . fields . inBatch ( batch ) . createFieldAsXml ( fieldsToCreate [ i ] ) ; } batch . execute ( ) . then ( async ( ) => { let addingStatus = [ ] ; for ( let i = 0 ; i < fieldsToCreate . length ; i ++ ) { const parser = new DOMParser ( ) ; const xml = parser . parseFromString ( fieldsToCreate [ i ] , 'text/xml' ) ; let fieldDisplayName = xml . querySelector ( 'Field' ) . getAttribute ( 'DisplayName' ) ; let listView = await listContext . views . getByTitle ( "All Items" ) . fields . add ( fieldDisplayName ) ; addingStatus . push ( listView ) ; } Promise . all ( addingStatus ) . then ( ( ) => { resolve ( "Success" ) ; } ) ; } ) ; } catch ( error ) { console . error ( "CommonServices_createListFields_FailedToCreatedField \n" , error ) ; reject ( "Failed" ) ; } } ) ; } public async getActiveTournamentDetails ( ) { let filterActive = "Status eq '" + stringsConstants . TournamentStatusActive + "'" ; const activeTournamentsArray = await this . getItemsWithOnlyFilter ( stringsConstants . TournamentsMasterList , filterActive ) ; return activeTournamentsArray ; } public async getAllBadgeImages ( listName , userEmail ) { var badgeImagesArray = [ ] ; var finalImagesArray = [ ] ; const filterFields = await sp . web . lists . getByTitle ( stringsConstants . DigitalBadgeLibrary ) . fields . filter ( "Title eq 'Tournament'" ) . get ( ) ; if ( filterFields . length == 0 ) { badgeImagesArray = await sp . web . lists . getByTitle ( stringsConstants . DigitalBadgeLibrary ) . items . select ( "Title" , "File/Name" ) . expand ( "File" ) . get ( ) ; for ( let i = 0 ; i < badgeImagesArray . length ; i ++ ) { finalImagesArray . push ( { title : badgeImagesArray [ i ] . Title , url : rootSiteURL + "/" + listName + "/" + badgeImagesArray [ i ] . File . Name } ) ; } } else { badgeImagesArray = await sp . web . lists . getByTitle ( stringsConstants . DigitalBadgeLibrary ) . items . select ( "Title" , "Tournament/Title" , "File/Name" ) . expand ( "Tournament" , "File" ) . get ( ) ; for ( let i = 0 ; i < badgeImagesArray . length ; i ++ ) { if ( badgeImagesArray [ i ] . Tournament == undefined ) { finalImagesArray . push ( { title : badgeImagesArray [ i ] . Title , url : rootSiteURL + "/" + listName + "/" + badgeImagesArray [ i ] . File . Name } ) ; } else { var tournamentCompleted = await this . getTournamentCompletedFlag ( badgeImagesArray [ i ] . Tournament . Title , userEmail ) ; if ( tournamentCompleted ) finalImagesArray . push ( { title : badgeImagesArray [ i ] . Title , url : rootSiteURL + "/" + listName + "/" + badgeImagesArray [ i ] . File . Name } ) ; } } } return finalImagesArray ; } public async getTournamentCompletedFlag ( tournamentName , currentUserEmail ) { let tournamentCompletedFlag = false ; tournamentName = tournamentName . replace ( /'/g , "''" ) ; let filterTournamentActions = "Title eq '" + tournamentName + "'" ; var tournamentActionsCount = 0 ; tournamentActionsCount = ( await sp . web . lists . getByTitle ( stringsConstants . TournamentActionsMasterList ) . items . filter ( filterTournamentActions ) . select ( "Title" ) . getAll ( ) ) . length ; let filterUserActions = "Tournament_x0020_Name eq '" + tournamentName + "'" + " and Title eq '" + currentUserEmail + "'" ; var userActionsCount = 0 ; userActionsCount = ( await sp . web . lists . getByTitle ( stringsConstants . UserActionsList ) . items . filter ( filterUserActions ) . select ( "Title" ) . getAll ( ) ) . length ; if ( tournamentActionsCount == userActionsCount ) tournamentCompletedFlag = true ; return tournamentCompletedFlag ; } public async getUserActions ( activeTournamentName , allUsersDetails ) { return new Promise < any > ( async ( resolve , reject ) => { try { let userActionsWithDisplayName = [ ] ; let userRanks = [ ] ; let getAllUserActions = [ ] ; let filterQuery = "Tournament_x0020_Name eq '" + activeTournamentName . replace ( /'/g , "''" ) + "'" ; let getUserActions = await sp . web . lists . getByTitle ( stringsConstants . UserActionsList ) . items . filter ( filterQuery ) . select ( "Title" , "Points" ) . top ( 5000 ) . getPaged ( ) ; if ( getUserActions . results . length > 0 ) { getAllUserActions . push ( ... getUserActions . results ) ; while ( getUserActions . hasNext ) { getUserActions = await getUserActions . getNext ( ) ; getAllUserActions . push ( ... getUserActions . results ) ; } var groupOfUniqueUsers = [ ] ; getAllUserActions . reduce ( ( res , value ) => { if ( ! res [ value . Title ] ) { res [ value . Title ] = { Title : value . Title , Points : 0 } ; groupOfUniqueUsers . push ( res [ value . Title ] ) ; } res [ value . Title ] . Points += value . Points ; return res ; } , { } ) ; groupOfUniqueUsers . sort ( ( a , b ) => { if ( a . Points < b . Points ) return 1 ; if ( a . Points > b . Points ) return - 1 ; if ( a . Title > b . Title ) return 1 ; if ( a . Title < b . Title ) return - 1 ; } ) ; if ( groupOfUniqueUsers . length > 0 ) { for ( let i = 0 ; i < groupOfUniqueUsers . length ; i ++ ) { let itemEmail = groupOfUniqueUsers [ i ] . Title . toLowerCase ( ) ; let userDisplayName = allUsersDetails . filter ( ( user ) => user . email === itemEmail ) ; if ( userDisplayName . length > 0 ) { userActionsWithDisplayName . push ( { User : userDisplayName [ 0 ] . displayName . replace ( ',' , '' ) , Points : groupOfUniqueUsers [ i ] . Points , Email : itemEmail } ) ; } } } for ( let j = 0 ; j < userActionsWithDisplayName . length ; j ++ ) { userRanks . push ( { Rank : j + 1 , User : userActionsWithDisplayName [ j ] . User , Points : userActionsWithDisplayName [ j ] . Points , Email : userActionsWithDisplayName [ j ] . Email } ) ; } resolve ( userRanks ) ; } else { resolve ( userRanks ) ; } } catch ( error ) { console . error ( "CommonServices_getUserActions \n" , error ) ; reject ( "Failed" ) ; } } ) ; } }