-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add drive object to onedrive integration
- Loading branch information
Showing
12 changed files
with
476 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/api/src/filestorage/drive/services/sharepoint/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { EncryptionService } from '@@core/@core-services/encryption/encryption.service'; | ||
import { LoggerService } from '@@core/@core-services/logger/logger.service'; | ||
import { PrismaService } from '@@core/@core-services/prisma/prisma.service'; | ||
import { ApiResponse } from '@@core/utils/types'; | ||
import { SyncParam } from '@@core/utils/types/interface'; | ||
import { FileStorageObject } from '@filestorage/@lib/@types'; | ||
import { IDriveService } from '@filestorage/drive/types'; | ||
import { Injectable } from '@nestjs/common'; | ||
import axios from 'axios'; | ||
import { ServiceRegistry } from '../registry.service'; | ||
import { SharepointDriveOutput } from './types'; | ||
import { DesunifyReturnType } from '@@core/utils/types/desunify.input'; | ||
import { OriginalDriveOutput } from '@@core/utils/types/original/original.file-storage'; | ||
|
||
@Injectable() | ||
export class SharepointService implements IDriveService { | ||
constructor( | ||
private prisma: PrismaService, | ||
private logger: LoggerService, | ||
private cryptoService: EncryptionService, | ||
private registry: ServiceRegistry, | ||
) { | ||
this.logger.setContext( | ||
`${FileStorageObject.file.toUpperCase()}:${SharepointService.name}`, | ||
); | ||
this.registry.registerService('sharepoint', this); | ||
} | ||
|
||
async addDrive( | ||
driveData: DesunifyReturnType, | ||
linkedUserId: string, | ||
): Promise<ApiResponse<OriginalDriveOutput>> { | ||
// No API to add drive in Sharepoint | ||
return; | ||
} | ||
|
||
async sync(data: SyncParam): Promise<ApiResponse<SharepointDriveOutput[]>> { | ||
try { | ||
const { linkedUserId } = data; | ||
|
||
const connection = await this.prisma.connections.findFirst({ | ||
where: { | ||
id_linked_user: linkedUserId, | ||
provider_slug: 'sharepoint', | ||
vertical: 'filestorage', | ||
}, | ||
}); | ||
|
||
const resp = await axios.get(`${connection.account_url}/drives`, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${this.cryptoService.decrypt( | ||
connection.access_token, | ||
)}`, | ||
}, | ||
}); | ||
|
||
const drives: SharepointDriveOutput[] = resp.data.value; | ||
this.logger.log(`Synced sharepoint drives !`); | ||
|
||
return { | ||
data: drives, | ||
message: 'Sharepoint drives retrived', | ||
statusCode: 200, | ||
}; | ||
} catch (error) { | ||
console.log(error.response); | ||
throw error; | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
packages/api/src/filestorage/drive/services/sharepoint/mappers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { MappersRegistry } from '@@core/@core-services/registries/mappers.registry'; | ||
import { CoreUnification } from '@@core/@core-services/unification/core-unification.service'; | ||
import { Utils } from '@filestorage/@lib/@utils'; | ||
import { | ||
UnifiedFilestorageDriveInput, | ||
UnifiedFilestorageDriveOutput, | ||
} from '@filestorage/drive/types/model.unified'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { SharepointDriveInput, SharepointDriveOutput } from './types'; | ||
import { IDriveMapper } from '@filestorage/drive/types'; | ||
|
||
@Injectable() | ||
export class SharepointDriveMapper implements IDriveMapper { | ||
constructor( | ||
private mappersRegistry: MappersRegistry, | ||
private utils: Utils, | ||
private coreUnificationService: CoreUnification, | ||
) { | ||
this.mappersRegistry.registerService( | ||
'filestorage', | ||
'drive', | ||
'sharepoint', | ||
this, | ||
); | ||
} | ||
|
||
async desunify( | ||
source: UnifiedFilestorageDriveInput, | ||
customFieldMappings?: { | ||
slug: string; | ||
remote_id: string; | ||
}[], | ||
): Promise<SharepointDriveInput> { | ||
return; | ||
} | ||
|
||
async unify( | ||
source: SharepointDriveOutput | SharepointDriveOutput[], | ||
connectionId: string, | ||
customFieldMappings?: { | ||
slug: string; | ||
remote_id: string; | ||
}[], | ||
): Promise<UnifiedFilestorageDriveOutput | UnifiedFilestorageDriveOutput[]> { | ||
if (!Array.isArray(source)) { | ||
return await this.mapSingleDriveToUnified( | ||
source, | ||
connectionId, | ||
customFieldMappings, | ||
); | ||
} | ||
// Handling array of SharepointDriveOutput | ||
return Promise.all( | ||
source.map((drive) => | ||
this.mapSingleDriveToUnified(drive, connectionId, customFieldMappings), | ||
), | ||
); | ||
} | ||
|
||
private async mapSingleDriveToUnified( | ||
drive: SharepointDriveOutput, | ||
connectionId: string, | ||
customFieldMappings?: { | ||
slug: string; | ||
remote_id: string; | ||
}[], | ||
): Promise<UnifiedFilestorageDriveOutput> { | ||
const field_mappings: { [key: string]: any } = {}; | ||
if (customFieldMappings) { | ||
for (const mapping of customFieldMappings) { | ||
field_mappings[mapping.slug] = drive[mapping.remote_id]; | ||
} | ||
} | ||
|
||
const result: UnifiedFilestorageDriveOutput = { | ||
remote_id: drive.id, | ||
remote_data: drive, | ||
name: drive.name, | ||
remote_created_at: drive.createdDateTime, | ||
drive_url: drive.webUrl, | ||
field_mappings, | ||
}; | ||
|
||
return result; | ||
} | ||
} |
Oops, something went wrong.