import { Controller, Get, Headers, Query, StreamableFile, UseGuards } from '@nestjs/common'; // import { Client as MsGraphClient } from '@microsoft/microsoft-graph-client'; import { MsGraphService } from '@noahspan/noahspan-modules' @Controller() export class AppController { constructor( private readonly msGraphService: MsGraphService ) {} @Get('userPhoto') async getProfilePhoto(@Headers() headers: any): Promise { // try { // const graphToken: string = await this.msGraphService.getMsGraphAuth( // headers.authorization.replace('Bearer ', ''), // ['user.read'] // ); // const client: MsGraphClient = // await this.msGraphService.getMsGraphClientDelegated(graphToken); // const blob: Blob = await client.api(`me/photos('48x48')/$value`).get(); // const arrayBuffer: ArrayBuffer = await blob.arrayBuffer(); // const buffer: Buffer = Buffer.from(arrayBuffer); // return new StreamableFile(buffer, { // type: 'application/json', // disposition: `attachment; filename="user_photo.png"` // }); // } catch (error) { // return error; // } try { const buffer = await this.msGraphService.getProfilePhoto( headers.authorization.replace('Bearer ', ''), ['user.read'] ) return new StreamableFile(buffer, { type: 'application/json', disposition: `attachment; filename="user_photo.png"` }); } catch (error) { return error; } } @Get('userProfile') async getUserProfile(@Headers() headers: any): Promise { // try { // const graphToken: string = await this.msGraphService.getMsGraphAuth( // headers.authorization.replace('Bearer ', ''), // ['user.read'] // ); // const client: MsGraphClient = // await this.msGraphService.getMsGraphClientDelegated(graphToken); // const userProfile = await client.api(`me`).get(); // return userProfile; // } catch (error) { // return error; // } try { const userProfile = await this.msGraphService.getUserProfile(headers.authorization.replace('Bearer ', '')); return userProfile; } catch (error) { return error; } } }