forked from panoratech/Panora
-
Notifications
You must be signed in to change notification settings - Fork 0
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 for one-drive integration
- Loading branch information
Showing
11 changed files
with
428 additions
and
41 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
70 changes: 70 additions & 0 deletions
70
packages/api/src/filestorage/drive/services/onedrive/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,70 @@ | ||
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 { OnedriveDriveOutput } from './types'; | ||
import { DesunifyReturnType } from '@@core/utils/types/desunify.input'; | ||
import { OriginalDriveOutput } from '@@core/utils/types/original/original.file-storage'; | ||
|
||
@Injectable() | ||
export class OnedriveService implements IDriveService { | ||
constructor( | ||
private prisma: PrismaService, | ||
private logger: LoggerService, | ||
private cryptoService: EncryptionService, | ||
private registry: ServiceRegistry, | ||
) { | ||
this.logger.setContext( | ||
`${FileStorageObject.file.toUpperCase()}:${OnedriveService.name}`, | ||
); | ||
this.registry.registerService('onedrive', this); | ||
} | ||
|
||
async addDrive( | ||
driveData: DesunifyReturnType, | ||
linkedUserId: string, | ||
): Promise<ApiResponse<OriginalDriveOutput>> { | ||
// No API to add drive in onedrive | ||
return; | ||
} | ||
|
||
async sync(data: SyncParam): Promise<ApiResponse<OnedriveDriveOutput[]>> { | ||
try { | ||
const { linkedUserId } = data; | ||
|
||
const connection = await this.prisma.connections.findFirst({ | ||
where: { | ||
id_linked_user: linkedUserId, | ||
provider_slug: 'onedrive', | ||
vertical: 'filestorage', | ||
}, | ||
}); | ||
|
||
const resp = await axios.get(`${connection.account_url}/v1.0/drives`, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${this.cryptoService.decrypt( | ||
connection.access_token, | ||
)}`, | ||
}, | ||
}); | ||
|
||
const drives: OnedriveDriveOutput[] = resp.data.value; | ||
this.logger.log(`Synced onedrive drives !`); | ||
|
||
return { | ||
data: drives, | ||
message: 'Onedrive drive retrived', | ||
statusCode: 200, | ||
}; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
packages/api/src/filestorage/drive/services/onedrive/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 { OnedriveDriveInput, OnedriveDriveOutput } from './types'; | ||
import { IDriveMapper } from '@filestorage/drive/types'; | ||
|
||
@Injectable() | ||
export class OnedriveDriveMapper implements IDriveMapper { | ||
constructor( | ||
private mappersRegistry: MappersRegistry, | ||
private utils: Utils, | ||
private coreUnificationService: CoreUnification, | ||
) { | ||
this.mappersRegistry.registerService( | ||
'filestorage', | ||
'drive', | ||
'onedrive', | ||
this, | ||
); | ||
} | ||
|
||
async desunify( | ||
source: UnifiedFilestorageDriveInput, | ||
customFieldMappings?: { | ||
slug: string; | ||
remote_id: string; | ||
}[], | ||
): Promise<OnedriveDriveInput> { | ||
return; | ||
} | ||
|
||
async unify( | ||
source: OnedriveDriveOutput | OnedriveDriveOutput[], | ||
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 OnedriveDriveOutput | ||
return Promise.all( | ||
source.map((drive) => | ||
this.mapSingleDriveToUnified(drive, connectionId, customFieldMappings), | ||
), | ||
); | ||
} | ||
|
||
private async mapSingleDriveToUnified( | ||
drive: OnedriveDriveOutput, | ||
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; | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
packages/api/src/filestorage/drive/services/onedrive/types.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,130 @@ | ||
/** | ||
* Represents the response from the OneDrive API for a specific drive. | ||
* @see https://learn.microsoft.com/en-us/graph/api/resources/drive?view=graph-rest-1.0 | ||
*/ | ||
export interface OnedriveDriveOutput { | ||
/** The date and time when the drive was created. */ | ||
readonly createdDateTime: string; | ||
/** A user-visible description of the drive. */ | ||
description: string; | ||
/** The unique identifier of the drive. */ | ||
readonly id: string; | ||
/** The date and time when the drive was last modified. */ | ||
readonly lastModifiedDateTime: string; | ||
/** The name of the drive. */ | ||
name: string; | ||
/** URL that displays the resource in the browser. */ | ||
readonly webUrl: string; | ||
/** Describes the type of drive represented by this resource. */ | ||
readonly driveType: 'personal' | 'business' | 'documentLibrary'; | ||
/** Identity of the user, device, or application which created the item. Read-only. */ | ||
readonly createdBy: IdentitySet; | ||
/** Identity of the user, device, and application which last modified the item. Read-only. */ | ||
readonly lastModifiedBy: IdentitySet; | ||
/** The user account that owns the drive. */ | ||
readonly owner?: IdentitySet; | ||
/** Information about the drive's storage space quota. */ | ||
readonly quota?: Quota; | ||
/** SharePoint identifiers for REST compatibility. */ | ||
readonly sharepointIds?: SharepointIds; | ||
/** Indicates that this is a system-managed drive. */ | ||
readonly system?: SystemFacet; | ||
} | ||
|
||
/** | ||
* Represents a set of identities, such as user, device, or application identities. | ||
* @see https://learn.microsoft.com/en-us/graph/api/resources/identityset?view=graph-rest-1.0 | ||
*/ | ||
export interface IdentitySet { | ||
/** Identity representing an application. */ | ||
readonly application?: Identity; | ||
/** Identity representing an application instance. */ | ||
readonly applicationInstance?: Identity; | ||
/** Identity representing a conversation. */ | ||
readonly conversation?: Identity; | ||
/** Identity representing a conversation identity type. */ | ||
readonly conversationIdentityType?: Identity; | ||
/** Identity representing a device. */ | ||
readonly device?: Identity; | ||
/** Identity representing encrypted identity information. */ | ||
readonly encrypted?: Identity; | ||
/** Identity representing an on-premises identity. */ | ||
readonly onPremises?: Identity; | ||
/** Identity representing a guest user. */ | ||
readonly guest?: Identity; | ||
/** Identity representing a phone identity. */ | ||
readonly phone?: Identity; | ||
/** Identity representing a user. */ | ||
readonly user?: Identity; | ||
} | ||
|
||
/** | ||
* Represents a generic identity used in various identity sets. | ||
* @see https://learn.microsoft.com/en-us/graph/api/resources/identity?view=graph-rest-1.0 | ||
*/ | ||
export interface Identity { | ||
/** The display name of the identity. */ | ||
readonly displayName?: string; | ||
/** The ID of the identity. */ | ||
readonly id?: string; | ||
/** The identity type (such as user, application, or device). */ | ||
readonly identityType?: string; | ||
/** The email address of the identity. */ | ||
readonly email?: string; | ||
} | ||
|
||
/** | ||
* Represents the storage quota information of a drive. | ||
*/ | ||
export interface Quota { | ||
/** The total number of bytes deleted from the drive. */ | ||
readonly deleted: number; | ||
/** The total number of bytes remaining in the drive's quota. */ | ||
readonly remaining: number; | ||
/** The state of the drive's quota (e.g., normal, nearing, exceeded). */ | ||
readonly state: 'normal' | 'nearing' | 'critical' | 'exceeded'; | ||
/** The total number of bytes in the drive's quota. */ | ||
readonly total: number; | ||
/** The total number of bytes used in the drive. */ | ||
readonly used: number; | ||
/** Information about storage plan upgrades, if available. */ | ||
readonly storagePlanInformation?: StoragePlanInformation; | ||
} | ||
|
||
/** | ||
* Represents storage plan upgrade information. | ||
*/ | ||
export interface StoragePlanInformation { | ||
/** Indicates whether an upgrade is available for the storage plan. */ | ||
readonly upgradeAvailable: boolean; | ||
} | ||
|
||
/** | ||
* Represents SharePoint-specific identifiers for an item. | ||
*/ | ||
export interface SharepointIds { | ||
/** The unique identifier (GUID) for the item's list in SharePoint. */ | ||
readonly listId: string; | ||
/** An integer identifier for the item within the containing list. */ | ||
readonly listItemId: string; | ||
/** The unique identifier (GUID) for the item within OneDrive for Business or a SharePoint site. */ | ||
readonly listItemUniqueId: string; | ||
/** The unique identifier (GUID) for the item's site collection (SPSite). */ | ||
readonly siteId: string; | ||
/** The SharePoint URL for the site that contains the item. */ | ||
readonly siteUrl: string; | ||
/** The unique identifier (GUID) for the tenancy. */ | ||
readonly tenantId: string; | ||
/** The unique identifier (GUID) for the item's site (SPWeb). */ | ||
readonly webId: string; | ||
} | ||
|
||
/** | ||
* Represents system-related metadata for the drive. | ||
*/ | ||
export interface SystemFacet { | ||
// Add properties specific to the system facet if needed. | ||
readonly [key: string]: any; | ||
} | ||
|
||
export type OnedriveDriveInput = Partial<OnedriveDriveOutput>; |
Oops, something went wrong.