@geniehr/utilities 1.0.10 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,8 @@
5
5
  export declare function getAWSParameters(nameOrPrefix: string): Promise<Record<string, string>>;
6
6
  export declare function matchFace(imageInBase64: string): Promise<import("@aws-sdk/client-rekognition").SearchFacesByImageCommandOutput>;
7
7
  export declare function registerFace(employeeId: string, imageInBase64: string): Promise<import("@aws-sdk/client-rekognition").IndexFacesCommandOutput>;
8
- export declare function generatePresignedUrl(entity: string, fileName: string, bucketName: string, idToken: string, action: 'put' | 'get', expiresIn?: number): Promise<string>;
8
+ export declare function generatePresignedUrl(bucketName: string, key: string, idToken: string, action: 'put' | 'get', expiresIn?: number): Promise<string>;
9
+ export declare function generatePresignedUrlLegacy(entity: string, fileName: string, bucketName: string, idToken: string, action: 'put' | 'get', expiresIn?: number): Promise<string>;
9
10
  export declare function executeS3Action(action: "put" | "get", bucketName: string, key: string, body?: Buffer, contentType?: string): Promise<import("@aws-sdk/client-s3").PutObjectCommandOutput>;
10
11
  export declare function updateCustomAttribute(username: string, userPoolId: string, attributeName: string, attributeValue: string): Promise<{
11
12
  success: boolean;
@@ -83,14 +83,40 @@ export async function registerFace(employeeId, imageInBase64) {
83
83
  const result = await rekognition.send(command);
84
84
  return result;
85
85
  }
86
- export async function generatePresignedUrl(entity, fileName, bucketName, idToken, action, expiresIn = 3600) {
86
+ export async function generatePresignedUrl(bucketName, key, idToken, action, expiresIn = 3600) {
87
+ let token = idToken?.startsWith("Bearer ") ? idToken.split(" ")[1] : idToken;
88
+ const payload = JSON.parse(Buffer.from(token.split(".")[1], "base64").toString());
89
+ const userId = Number(payload["custom:id"]);
90
+ if (!userId)
91
+ throw new AppException("User ID not found in token", "E-User-ID-Not-Found");
92
+ const REGION = "ap-south-1";
93
+ let command;
94
+ switch (action) {
95
+ case 'put':
96
+ command = new PutObjectCommand({
97
+ Bucket: bucketName,
98
+ Key: key,
99
+ });
100
+ break;
101
+ case 'get': command = new GetObjectCommand({
102
+ Bucket: bucketName,
103
+ Key: key,
104
+ });
105
+ }
106
+ try {
107
+ return await getSignedUrl(s3, command, { expiresIn });
108
+ }
109
+ catch (error) {
110
+ throw new AppException(`Failed to generate presigned URL: ${error instanceof Error ? error.message : "Unknown error"}`, "E-Presigned-URL-Generation-Failed");
111
+ }
112
+ }
113
+ export async function generatePresignedUrlLegacy(entity, fileName, bucketName, idToken, action, expiresIn = 3600) {
87
114
  let token = idToken?.startsWith("Bearer ") ? idToken.split(" ")[1] : idToken;
88
115
  const payload = JSON.parse(Buffer.from(token.split(".")[1], "base64").toString());
89
116
  const userId = Number(payload["custom:id"]);
90
117
  if (!userId)
91
118
  throw new AppException("User ID not found in token", "E-User-ID-Not-Found");
92
119
  const REGION = "ap-south-1";
93
- const s3 = new S3Client({ region: REGION });
94
120
  const key = `${userId}/${entity}/${fileName}`;
95
121
  let command;
96
122
  switch (action) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geniehr/utilities",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/Genie-HR/ghr-utilities#readme",
6
6
  "bugs": {