plenna_utilities 1.0.4 → 1.0.6

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.
package/package.json CHANGED
@@ -1,14 +1,9 @@
1
1
  {
2
2
  "name": "plenna_utilities",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "plenna's utils for backend projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
- "files": [
8
- "dist/",
9
- "package.json",
10
- "package-lock.json"
11
- ],
12
7
  "scripts": {
13
8
  "build": "tsc"
14
9
  },
@@ -1,28 +0,0 @@
1
- import docs from '@googleapis/docs';
2
- export declare class PlennaGoogleService {
3
- private credentials;
4
- constructor(credentials?: string);
5
- authorization(): Promise<import("google-auth-library").GoogleAuth<import("google-auth-library/build/src/auth/googleauth").JSONClient>>;
6
- getDocument(documentId: string): Promise<import("gaxios").GaxiosResponse<docs.docs_v1.Schema$Document> | undefined>;
7
- cloneDocument(documentId: string, folderId: string): Promise<{
8
- success: boolean;
9
- fileId?: string;
10
- }>;
11
- updateDocument(id: string, requests?: docs.docs_v1.Schema$Request[]): Promise<{
12
- success: boolean;
13
- updated: import("gaxios").GaxiosResponse<docs.docs_v1.Schema$BatchUpdateDocumentResponse>;
14
- } | {
15
- success: boolean;
16
- updated?: undefined;
17
- }>;
18
- exportDocument(fileId: string): Promise<ArrayBuffer | undefined>;
19
- deleteDocument(fileId: string): Promise<{
20
- success: boolean;
21
- deleted: import("gaxios").GaxiosResponse<void>;
22
- error?: undefined;
23
- } | {
24
- success: boolean;
25
- error: unknown;
26
- deleted?: undefined;
27
- }>;
28
- }
@@ -1,109 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.PlennaGoogleService = void 0;
7
- const docs_1 = __importDefault(require("@googleapis/docs"));
8
- const drive_1 = require("@googleapis/drive");
9
- const { getSecret } = require('./secrets');
10
- class PlennaGoogleService {
11
- credentials;
12
- constructor(credentials) {
13
- this.credentials = credentials;
14
- }
15
- async authorization() {
16
- let cred = this.credentials ?? '';
17
- if (cred === '') {
18
- console.log('getting credentials');
19
- cred = await getSecret('GoogleAuth') ?? '';
20
- }
21
- return new docs_1.default.auth.GoogleAuth({
22
- credentials: JSON.parse(cred),
23
- scopes: [
24
- 'https://www.googleapis.com/auth/documents',
25
- 'https://www.googleapis.com/auth/drive'
26
- ]
27
- });
28
- }
29
- async getDocument(documentId) {
30
- const authClient = await this.authorization();
31
- const client = await docs_1.default.docs({ version: 'v1', auth: authClient });
32
- try {
33
- return await client.documents.get({ documentId });
34
- }
35
- catch (error) {
36
- console.log(error);
37
- }
38
- }
39
- async cloneDocument(documentId, folderId) {
40
- const authClient = await this.authorization();
41
- const gdrive = new drive_1.drive_v3.Drive({ auth: authClient });
42
- try {
43
- const fileId = await gdrive.files.copy({
44
- fileId: documentId,
45
- requestBody: {
46
- name: new Date().toISOString(),
47
- parents: [folderId]
48
- },
49
- fields: '*'
50
- });
51
- return { success: true, fileId: fileId?.data?.id ?? '' };
52
- }
53
- catch (error) {
54
- console.log(error);
55
- return { success: false };
56
- }
57
- }
58
- async updateDocument(id, requests = []) {
59
- const authClient = await this.authorization();
60
- const client = await docs_1.default.docs({ version: 'v1', auth: authClient });
61
- try {
62
- const updated = await client.documents.batchUpdate({
63
- documentId: id,
64
- requestBody: {
65
- requests: requests
66
- }
67
- });
68
- return { success: true, updated };
69
- }
70
- catch (error) {
71
- console.log(error);
72
- return { success: false };
73
- }
74
- }
75
- async exportDocument(fileId) {
76
- const authClient = await this.authorization();
77
- const gdrive = new drive_1.drive_v3.Drive({ auth: authClient });
78
- try {
79
- const response = await gdrive.files.get({ fileId, fields: 'exportLinks' });
80
- const exportLinks = response?.data?.exportLinks ?? {};
81
- const link = exportLinks['application/pdf'];
82
- if (link === undefined) {
83
- throw new Error('not valid link received');
84
- }
85
- const token = await authClient.getAccessToken();
86
- const file = await fetch(link, {
87
- headers: { Authorization: `Bearer ${token}` }
88
- });
89
- console.log({ file });
90
- return file.arrayBuffer();
91
- }
92
- catch (error) {
93
- console.log(error);
94
- }
95
- }
96
- async deleteDocument(fileId) {
97
- const authClient = await this.authorization();
98
- const gdrive = new drive_1.drive_v3.Drive({ auth: authClient });
99
- try {
100
- const deleted = await gdrive.files.delete({ fileId });
101
- return { success: true, deleted };
102
- }
103
- catch (error) {
104
- console.log(error);
105
- return { success: false, error };
106
- }
107
- }
108
- }
109
- exports.PlennaGoogleService = PlennaGoogleService;
@@ -1,28 +0,0 @@
1
- import { type APIGatewayProxyResult } from 'aws-lambda';
2
- export type httpCode = 200 | 400 | 401 | 403 | 404 | 500;
3
- export interface IPlennaStatus {
4
- code: number;
5
- message: string;
6
- httpCode: httpCode;
7
- }
8
- export declare class HttpStatusCode {
9
- httpCode: httpCode;
10
- constructor(code: httpCode);
11
- }
12
- export declare class Result {
13
- private readonly statusCode;
14
- private readonly code;
15
- private readonly message;
16
- private readonly data?;
17
- private readonly headers;
18
- constructor(statusCode: httpCode, code: number, message: string, data?: unknown, headers?: Record<string, unknown>);
19
- bodyToString(): APIGatewayProxyResult;
20
- }
21
- export declare class PlennaError extends Error implements HttpStatusCode {
22
- code: number;
23
- message: string;
24
- httpCode: httpCode;
25
- constructor(status: IPlennaStatus);
26
- }
27
- export declare const success: (data: object, headers?: Record<string, unknown>) => APIGatewayProxyResult;
28
- export declare const error: (httpStatusCode: httpCode, message: string, code?: number) => APIGatewayProxyResult;
@@ -1,64 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.error = exports.success = exports.PlennaError = exports.Result = exports.HttpStatusCode = void 0;
4
- class HttpStatusCode {
5
- httpCode;
6
- constructor(code) {
7
- this.httpCode = code;
8
- }
9
- }
10
- exports.HttpStatusCode = HttpStatusCode;
11
- class Result {
12
- statusCode;
13
- code;
14
- message;
15
- data;
16
- headers;
17
- constructor(statusCode, code, message, data, headers) {
18
- this.statusCode = statusCode;
19
- this.code = code;
20
- this.message = message;
21
- this.data = data;
22
- this.headers = { ...headers };
23
- }
24
- bodyToString() {
25
- const exposedHeaders = Object.entries(this.headers).map(([key]) => key).join(',');
26
- return {
27
- statusCode: this.statusCode,
28
- headers: {
29
- ...this.headers,
30
- 'Access-Control-Allow-Origin': '*',
31
- 'Access-Control-Expose-Headers': exposedHeaders,
32
- 'Access-Control-Allow-Credentials': true
33
- },
34
- body: JSON.stringify({
35
- code: this.code,
36
- message: this.message,
37
- data: this.data
38
- })
39
- };
40
- }
41
- }
42
- exports.Result = Result;
43
- class PlennaError extends Error {
44
- code;
45
- message;
46
- httpCode;
47
- constructor(status) {
48
- super();
49
- this.code = status.code;
50
- this.message = status.message;
51
- this.httpCode = status.httpCode;
52
- }
53
- }
54
- exports.PlennaError = PlennaError;
55
- const success = (data, headers) => {
56
- const result = new Result(200, 0, 'success', data, headers);
57
- return result.bodyToString();
58
- };
59
- exports.success = success;
60
- const error = (httpStatusCode, message, code = 0) => {
61
- const result = new Result(httpStatusCode, code, message);
62
- return result.bodyToString();
63
- };
64
- exports.error = error;
@@ -1,7 +0,0 @@
1
- import { ObjectCannedACL } from "@aws-sdk/client-s3";
2
- export declare class PlennaS3Service {
3
- private readonly bucket;
4
- private readonly acl;
5
- constructor(bucket: string, acl: ObjectCannedACL);
6
- getPresignedUrl(keyName: string): Promise<string>;
7
- }
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PlennaS3Service = void 0;
4
- const client_s3_1 = require("@aws-sdk/client-s3");
5
- const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
6
- class PlennaS3Service {
7
- bucket;
8
- acl;
9
- constructor(bucket, acl) {
10
- this.bucket = bucket;
11
- this.acl = acl;
12
- }
13
- async getPresignedUrl(keyName) {
14
- const s3Client = new client_s3_1.S3Client({ region: 'us-east-2' });
15
- const command = new client_s3_1.PutObjectCommand({
16
- Bucket: this.bucket,
17
- Key: keyName,
18
- ACL: this.acl
19
- });
20
- const signedUrl = await (0, s3_request_presigner_1.getSignedUrl)(s3Client, command, { expiresIn: 3600 });
21
- return signedUrl;
22
- }
23
- }
24
- exports.PlennaS3Service = PlennaS3Service;
@@ -1,6 +0,0 @@
1
- export declare const fixAppointmentDateStart: (date: Date, start: string) => Date;
2
- export declare const isValidNumeric: (value: number | undefined) => boolean;
3
- export declare const compare: (left: Date, right: Date) => boolean;
4
- export declare const getAge: (date: Date) => string;
5
- export declare const getDeltaTime: (start: Date, end: Date) => number;
6
- export declare const getMonthDifference: (startDate: Date, endDate: Date) => number;
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMonthDifference = exports.getDeltaTime = exports.getAge = exports.compare = exports.isValidNumeric = exports.fixAppointmentDateStart = void 0;
4
- const fixAppointmentDateStart = (date, start) => {
5
- const [hour, minutes] = start.split(':');
6
- const fixedDate = new Date(date).setUTCHours(parseInt(hour ?? 0) + 6, parseInt(minutes ?? 0));
7
- return new Date(fixedDate);
8
- };
9
- exports.fixAppointmentDateStart = fixAppointmentDateStart;
10
- const isValidNumeric = (value) => {
11
- return (typeof value === 'number' && !isNaN(value));
12
- };
13
- exports.isValidNumeric = isValidNumeric;
14
- const compare = (left, right) => {
15
- return left > right;
16
- };
17
- exports.compare = compare;
18
- const getAge = (date) => {
19
- if (date === null || date === undefined) {
20
- return 'Pendiente';
21
- }
22
- const diffMs = Date.now() - date.getTime();
23
- const ageDt = new Date(diffMs);
24
- return `${Math.abs(ageDt.getUTCFullYear() - 1970)}`;
25
- };
26
- exports.getAge = getAge;
27
- const getDeltaTime = (start, end) => {
28
- const diff = end.getTime() - start.getTime();
29
- const diffDays = diff / (1000 * 3600);
30
- return diffDays;
31
- };
32
- exports.getDeltaTime = getDeltaTime;
33
- const getMonthDifference = (startDate, endDate) => {
34
- const calculated = (endDate.getMonth() - startDate.getMonth() + 12 * (endDate.getFullYear() - startDate.getFullYear()));
35
- console.log({ startDate, endDate, calculated });
36
- return calculated;
37
- };
38
- exports.getMonthDifference = getMonthDifference;