-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex-functions.js
51 lines (43 loc) · 1.56 KB
/
index-functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// index-functions.js
// const { BlobServiceClient } = require("@azure/storage-blob");
// const configJSON = require("./config.json");
import { BlobServiceClient } from "@azure/storage-blob";
import configJSON from './config.json' assert { type: 'json' };
const connectionString = configJSON.blobConnectionString;
const containerName = configJSON.blobContainerName;
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
const containerClient = blobServiceClient.getContainerClient(containerName);
const sessionTableFile = "sessionTable.json";
let sessionTable = {};
async function getUserGridId (userId){
sessionTable = await downloadSessionTable();
let gridId = "";
for (const key in sessionTable) {
if (sessionTable[key].userId.slice(0, 5) === userId.slice(0, 5)) {
gridId = key;
}
}
console.log('sessiontTable gridId =', gridId)
return gridId
}
async function downloadSessionTable() {
let downloadedTable =""
try {
let blobClient = containerClient.getBlobClient(sessionTableFile);
let downloadedResponse = await blobClient.download();
let downloadedContent = await streamToString(downloadedResponse.readableStreamBody);
downloadedTable = JSON.parse(downloadedContent);
return downloadedTable;
} catch (error) {
throw error;
}
}
// Helper function to convert a ReadableStream to a string
async function streamToString(readableStream) {
const chunks = [];
for await (const chunk of readableStream) {
chunks.push(chunk.toString());
}
return chunks.join("");
}
export { getUserGridId };