@pick2me/shared 1.0.3
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/dist/auth/auth.d.ts +9 -0
- package/dist/auth/auth.d.ts.map +1 -0
- package/dist/auth/auth.js +82 -0
- package/dist/auth/index.d.ts +4 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +2 -0
- package/dist/auth/verifyGatewayJwt.d.ts +14 -0
- package/dist/auth/verifyGatewayJwt.d.ts.map +1 -0
- package/dist/auth/verifyGatewayJwt.js +47 -0
- package/dist/constants/index.d.ts +2 -0
- package/dist/constants/index.d.ts.map +1 -0
- package/dist/constants/index.js +1 -0
- package/dist/constants/redis-keys.d.ts +10 -0
- package/dist/constants/redis-keys.d.ts.map +1 -0
- package/dist/constants/redis-keys.js +14 -0
- package/dist/errors/HttpError.d.ts +17 -0
- package/dist/errors/HttpError.d.ts.map +1 -0
- package/dist/errors/HttpError.js +17 -0
- package/dist/errors/errorHandler.d.ts +3 -0
- package/dist/errors/errorHandler.d.ts.map +1 -0
- package/dist/errors/errorHandler.js +37 -0
- package/dist/errors/index.d.ts +10 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +9 -0
- package/dist/interfaces/common-response.d.ts +14 -0
- package/dist/interfaces/common-response.d.ts.map +1 -0
- package/dist/interfaces/common-response.js +1 -0
- package/dist/interfaces/common-types.d.ts +39 -0
- package/dist/interfaces/common-types.d.ts.map +1 -0
- package/dist/interfaces/common-types.js +1 -0
- package/dist/interfaces/index.d.ts +5 -0
- package/dist/interfaces/index.d.ts.map +1 -0
- package/dist/interfaces/index.js +4 -0
- package/dist/interfaces/rabbit-event-types.d.ts +6 -0
- package/dist/interfaces/rabbit-event-types.d.ts.map +1 -0
- package/dist/interfaces/rabbit-event-types.js +1 -0
- package/dist/interfaces/status-code.d.ts +66 -0
- package/dist/interfaces/status-code.d.ts.map +1 -0
- package/dist/interfaces/status-code.js +66 -0
- package/dist/messaging/index.d.ts +4 -0
- package/dist/messaging/index.d.ts.map +1 -0
- package/dist/messaging/index.js +3 -0
- package/dist/messaging/rabbitmq.config.d.ts +35 -0
- package/dist/messaging/rabbitmq.config.d.ts.map +1 -0
- package/dist/messaging/rabbitmq.config.js +43 -0
- package/dist/messaging/rabbitmq.d.ts +20 -0
- package/dist/messaging/rabbitmq.d.ts.map +1 -0
- package/dist/messaging/rabbitmq.js +65 -0
- package/dist/messaging/rabbitmq.utils.d.ts +3 -0
- package/dist/messaging/rabbitmq.utils.d.ts.map +1 -0
- package/dist/messaging/rabbitmq.utils.js +30 -0
- package/dist/mongo/connection.d.ts +2 -0
- package/dist/mongo/connection.d.ts.map +1 -0
- package/dist/mongo/connection.js +13 -0
- package/dist/mongo/i-mongo-base-repository.d.ts +16 -0
- package/dist/mongo/i-mongo-base-repository.d.ts.map +1 -0
- package/dist/mongo/i-mongo-base-repository.js +1 -0
- package/dist/mongo/index.d.ts +4 -0
- package/dist/mongo/index.d.ts.map +1 -0
- package/dist/mongo/index.js +3 -0
- package/dist/mongo/mongo-base-repository.d.ts +19 -0
- package/dist/mongo/mongo-base-repository.d.ts.map +1 -0
- package/dist/mongo/mongo-base-repository.js +114 -0
- package/dist/protos/booking.proto +22 -0
- package/dist/protos/driver.proto +450 -0
- package/dist/protos/index.d.ts +12 -0
- package/dist/protos/index.d.ts.map +1 -0
- package/dist/protos/index.js +30 -0
- package/dist/protos/index.ts +36 -0
- package/dist/protos/payment.proto +37 -0
- package/dist/protos/realtime.proto +19 -0
- package/dist/protos/user.proto +18 -0
- package/dist/redis/RedisService.d.ts +37 -0
- package/dist/redis/RedisService.d.ts.map +1 -0
- package/dist/redis/RedisService.js +203 -0
- package/dist/redis/client.d.ts +4 -0
- package/dist/redis/client.d.ts.map +1 -0
- package/dist/redis/client.js +7 -0
- package/dist/redis/index.d.ts +3 -0
- package/dist/redis/index.d.ts.map +1 -0
- package/dist/redis/index.js +2 -0
- package/dist/sql/i-sql-base-repository.d.ts +10 -0
- package/dist/sql/i-sql-base-repository.d.ts.map +1 -0
- package/dist/sql/i-sql-base-repository.js +1 -0
- package/dist/sql/index.d.ts +3 -0
- package/dist/sql/index.d.ts.map +1 -0
- package/dist/sql/index.js +2 -0
- package/dist/sql/sql-base-repository.d.ts +15 -0
- package/dist/sql/sql-base-repository.d.ts.map +1 -0
- package/dist/sql/sql-base-repository.js +74 -0
- package/dist/utils/asyncHandler.d.ts +3 -0
- package/dist/utils/asyncHandler.d.ts.map +1 -0
- package/dist/utils/asyncHandler.js +5 -0
- package/dist/utils/bcrypt.d.ts +5 -0
- package/dist/utils/bcrypt.d.ts.map +1 -0
- package/dist/utils/bcrypt.js +9 -0
- package/dist/utils/catchAsync.d.ts +3 -0
- package/dist/utils/catchAsync.d.ts.map +1 -0
- package/dist/utils/catchAsync.js +3 -0
- package/dist/utils/envChecker.d.ts +10 -0
- package/dist/utils/envChecker.d.ts.map +1 -0
- package/dist/utils/envChecker.js +26 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +3 -0
- package/package.json +119 -0
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import Redis from "ioredis";
|
|
2
|
+
import { GEO_KEY, GEO_KEY_RIDE, HEARTBEAT_PREFIX, IN_RIDE_HEARTBEAT_PREFIX, IN_RIDE_HEARTBEAT_PREFIX_DATA, ONLINE_DRIVER_DETAILS_PREFIX, } from "../constants/redis-keys";
|
|
3
|
+
export class RedisService {
|
|
4
|
+
static instance = null;
|
|
5
|
+
redis;
|
|
6
|
+
constructor(redisClient) {
|
|
7
|
+
this.redis = redisClient;
|
|
8
|
+
this.redis.on("error", (err) => {
|
|
9
|
+
console.error("Redis error:", err);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
static init(url) {
|
|
13
|
+
if (this.instance)
|
|
14
|
+
return this.instance;
|
|
15
|
+
if (!url) {
|
|
16
|
+
throw new Error("REDIS_URL is not set. Provide url to RedisService.init(url).");
|
|
17
|
+
}
|
|
18
|
+
const client = new Redis(url);
|
|
19
|
+
this.instance = new RedisService(client);
|
|
20
|
+
return this.instance;
|
|
21
|
+
}
|
|
22
|
+
static getInstance() {
|
|
23
|
+
if (!this.instance) {
|
|
24
|
+
throw new Error("RedisService not initialized. Call RedisService.init(url) first.");
|
|
25
|
+
}
|
|
26
|
+
return this.instance;
|
|
27
|
+
}
|
|
28
|
+
raw() {
|
|
29
|
+
return this.redis;
|
|
30
|
+
}
|
|
31
|
+
// ping / health
|
|
32
|
+
async ping() {
|
|
33
|
+
return this.redis.ping();
|
|
34
|
+
}
|
|
35
|
+
//BLACK LIST operations
|
|
36
|
+
async addBlacklistedToken(token, expSeconds) {
|
|
37
|
+
await this.redis.set(`blacklist:${token}`, "true", "EX", expSeconds);
|
|
38
|
+
}
|
|
39
|
+
async checkBlacklistedToken(token) {
|
|
40
|
+
const isBlacklisted = await this.redis.exists(`blacklist:${token}`);
|
|
41
|
+
return !!isBlacklisted;
|
|
42
|
+
}
|
|
43
|
+
async removeBlacklistedToken(token) {
|
|
44
|
+
const result = await this.redis.del(`blacklist:${token}`);
|
|
45
|
+
return result > 0;
|
|
46
|
+
}
|
|
47
|
+
// CRUD operation
|
|
48
|
+
async set(key, value, ttlSeconds = 30) {
|
|
49
|
+
await this.redis.set(key, value, "EX", ttlSeconds);
|
|
50
|
+
}
|
|
51
|
+
async get(key) {
|
|
52
|
+
return await this.redis.get(key);
|
|
53
|
+
}
|
|
54
|
+
async remove(key) {
|
|
55
|
+
const result = await this.redis.del(key);
|
|
56
|
+
return result > 0;
|
|
57
|
+
}
|
|
58
|
+
// public async setHeartbeat(
|
|
59
|
+
// driverId: string,
|
|
60
|
+
// ttl: number = 120
|
|
61
|
+
// ): Promise<void> {
|
|
62
|
+
// await this.redis.set(`${HEARTBEAT_PREFIX}${driverId}`, "1", "EX", ttl);
|
|
63
|
+
// }
|
|
64
|
+
// public async checkHeartbeat(driverId: string): Promise<boolean> {
|
|
65
|
+
// const exists = await this.redis.exists(`${HEARTBEAT_PREFIX}${driverId}`);
|
|
66
|
+
// return exists === 1;
|
|
67
|
+
// }
|
|
68
|
+
getHeartbeatKey(driverId, inRide = false) {
|
|
69
|
+
return `${inRide ? IN_RIDE_HEARTBEAT_PREFIX : HEARTBEAT_PREFIX}${driverId}`;
|
|
70
|
+
}
|
|
71
|
+
async setHeartbeat(driverId, ttl = 120, inRide = false, payload) {
|
|
72
|
+
await this.redis.set(this.getHeartbeatKey(driverId, inRide), "1", "EX", ttl);
|
|
73
|
+
if (inRide && payload) {
|
|
74
|
+
const heartbeatKey = `${IN_RIDE_HEARTBEAT_PREFIX_DATA}${driverId}`;
|
|
75
|
+
const now = new Date();
|
|
76
|
+
const midnight = new Date();
|
|
77
|
+
midnight.setHours(24, 0, 0, 0);
|
|
78
|
+
const ttl = Math.floor((midnight.getTime() - now.getTime()) / 1000);
|
|
79
|
+
await this.redis.set(heartbeatKey, JSON.stringify(payload), 'EX', ttl);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async getRideData(driverId) {
|
|
83
|
+
const payloadKey = `${IN_RIDE_HEARTBEAT_PREFIX_DATA}${driverId}`;
|
|
84
|
+
const data = await this.redis.hgetall(payloadKey);
|
|
85
|
+
if (Object.keys(data).length === 0) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
return data;
|
|
89
|
+
}
|
|
90
|
+
async checkHeartbeat(driverId, inRide = false) {
|
|
91
|
+
const exists = await this.redis.exists(this.getHeartbeatKey(driverId, inRide));
|
|
92
|
+
return exists === 1;
|
|
93
|
+
}
|
|
94
|
+
//online driver methods
|
|
95
|
+
async setOnlineDriver(driverDetails, location) {
|
|
96
|
+
const onlineDriverPrefix = `${ONLINE_DRIVER_DETAILS_PREFIX}${driverDetails.driverId}`;
|
|
97
|
+
const now = new Date();
|
|
98
|
+
const midnight = new Date();
|
|
99
|
+
midnight.setHours(24, 0, 0, 0);
|
|
100
|
+
const ttl = Math.floor((midnight.getTime() - now.getTime()) / 1000);
|
|
101
|
+
await this.redis.hset(onlineDriverPrefix, driverDetails);
|
|
102
|
+
await this.redis.expire(onlineDriverPrefix, ttl);
|
|
103
|
+
await this.redis.geoadd(GEO_KEY, location.longitude, location.latitude, driverDetails.driverId);
|
|
104
|
+
}
|
|
105
|
+
async getOnlineDriverDetails(driverId) {
|
|
106
|
+
const key = `${ONLINE_DRIVER_DETAILS_PREFIX}${driverId}`;
|
|
107
|
+
const data = await this.redis.hgetall(key);
|
|
108
|
+
if (!data || Object.keys(data).length === 0)
|
|
109
|
+
return null;
|
|
110
|
+
const details = {
|
|
111
|
+
driverId: data.driverId,
|
|
112
|
+
driverNumber: data.driverNumber,
|
|
113
|
+
name: data.name,
|
|
114
|
+
cancelledRides: Number(data.cancelledRides),
|
|
115
|
+
rating: Number(data.rating),
|
|
116
|
+
vehicleModel: data.vehicleModel,
|
|
117
|
+
driverPhoto: data.driverPhoto,
|
|
118
|
+
vehicleNumber: data.vehicleNumber,
|
|
119
|
+
stripeId: data.stripeId || undefined,
|
|
120
|
+
stripeLinkUrl: data.stripeLinkUrl || undefined,
|
|
121
|
+
sessionStart: data.sessionStart ? Number(data.sessionStart) : undefined,
|
|
122
|
+
lastSeen: data.lastSeen ? Number(data.lastSeen) : undefined,
|
|
123
|
+
};
|
|
124
|
+
return details;
|
|
125
|
+
}
|
|
126
|
+
async removeOnlineDriver(driverId) {
|
|
127
|
+
const onlineDriverPrefix = `${ONLINE_DRIVER_DETAILS_PREFIX}${driverId}`;
|
|
128
|
+
await this.redis.del(onlineDriverPrefix);
|
|
129
|
+
await this.redis.zrem(GEO_KEY, driverId);
|
|
130
|
+
}
|
|
131
|
+
// online driver geo method
|
|
132
|
+
async updateDriverGeo(driverId, location, inride = false) {
|
|
133
|
+
const key = inride ? GEO_KEY_RIDE : GEO_KEY;
|
|
134
|
+
await this.redis.geoadd(key, location.longitude, location.latitude, driverId);
|
|
135
|
+
}
|
|
136
|
+
async removeOnlineDriverGeo(driverId) {
|
|
137
|
+
await this.redis.zrem(GEO_KEY, driverId);
|
|
138
|
+
}
|
|
139
|
+
async findNearbyDrivers(latitude, longitude, radiusKm, limit = 20) {
|
|
140
|
+
const raw = (await this.redis.geosearch(GEO_KEY, "FROMLONLAT", longitude, latitude, "BYRADIUS", radiusKm, "km", "ASC", "WITHDIST", "WITHCOORD", "COUNT", limit));
|
|
141
|
+
if (!raw || raw.length === 0)
|
|
142
|
+
return [];
|
|
143
|
+
return raw
|
|
144
|
+
.map((entry) => {
|
|
145
|
+
// expected entry: [ member, distStr, [lonStr, latStr] ]
|
|
146
|
+
const member = entry[0];
|
|
147
|
+
const distStr = entry[1];
|
|
148
|
+
const coordArr = entry[2];
|
|
149
|
+
if (!member || !distStr || !Array.isArray(coordArr))
|
|
150
|
+
return null;
|
|
151
|
+
const longitudeParsed = Number(coordArr[0]);
|
|
152
|
+
const latitudeParsed = Number(coordArr[1]);
|
|
153
|
+
if (!Number.isFinite(longitudeParsed) ||
|
|
154
|
+
!Number.isFinite(latitudeParsed)) {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
return {
|
|
158
|
+
driverId: String(member),
|
|
159
|
+
distanceKm: Number(distStr),
|
|
160
|
+
longitude: longitudeParsed,
|
|
161
|
+
latitude: latitudeParsed,
|
|
162
|
+
};
|
|
163
|
+
})
|
|
164
|
+
.filter((x) => x !== null);
|
|
165
|
+
}
|
|
166
|
+
async getDriverGeoPosition(driverId, fromInRide = false) {
|
|
167
|
+
const key = fromInRide ? GEO_KEY_RIDE : GEO_KEY;
|
|
168
|
+
const pos = await this.redis.geopos(key, driverId);
|
|
169
|
+
if (!pos || !Array.isArray(pos) || !pos[0])
|
|
170
|
+
return null;
|
|
171
|
+
const lonStr = pos[0][0];
|
|
172
|
+
const latStr = pos[0][1];
|
|
173
|
+
if (!lonStr || !latStr)
|
|
174
|
+
return null;
|
|
175
|
+
const longitude = Number(lonStr);
|
|
176
|
+
const latitude = Number(latStr);
|
|
177
|
+
if (!Number.isFinite(longitude) || !Number.isFinite(latitude))
|
|
178
|
+
return null;
|
|
179
|
+
return { latitude, longitude };
|
|
180
|
+
}
|
|
181
|
+
async moveDriverToInRideGeo(driverId, location) {
|
|
182
|
+
let loc = location ?? (await this.getDriverGeoPosition(driverId, false));
|
|
183
|
+
if (!loc) {
|
|
184
|
+
loc = await this.getDriverGeoPosition(driverId, true);
|
|
185
|
+
}
|
|
186
|
+
const multi = this.redis.multi();
|
|
187
|
+
multi.zrem(GEO_KEY, driverId);
|
|
188
|
+
if (loc) {
|
|
189
|
+
multi.geoadd(GEO_KEY_RIDE, loc.longitude, loc.latitude, driverId);
|
|
190
|
+
console.log("add geo key");
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
console.warn("no location found");
|
|
194
|
+
}
|
|
195
|
+
await multi.exec();
|
|
196
|
+
}
|
|
197
|
+
async moveDriverOutOfInRideGeo(driverId, location) {
|
|
198
|
+
await this.redis.zrem(GEO_KEY_RIDE, driverId);
|
|
199
|
+
if (location) {
|
|
200
|
+
await this.redis.geoadd(GEO_KEY, location.longitude, location.latitude, driverId);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/redis/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,gBAElD;AAED,wBAAgB,eAAe,IAAI,YAAY,CAE9C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/redis/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,gBAAgB,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { DeepPartial } from "typeorm";
|
|
2
|
+
export interface ISqlBaseRepository<T> {
|
|
3
|
+
findOne(where: Partial<T>): Promise<T | null>;
|
|
4
|
+
findAll(where?: Partial<T>): Promise<T[] | null>;
|
|
5
|
+
create(data: DeepPartial<T>): Promise<T | null>;
|
|
6
|
+
update(id: string, data: Partial<T>): Promise<T | null>;
|
|
7
|
+
delete(id: string): Promise<boolean>;
|
|
8
|
+
findById(id: string): Promise<T | null>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=i-sql-base-repository.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"i-sql-base-repository.d.ts","sourceRoot":"","sources":["../../src/sql/i-sql-base-repository.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACtC,MAAM,WAAW,kBAAkB,CAAC,CAAC;IACnC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;IAC9C,OAAO,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC;IACjD,MAAM,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;IAChD,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;IACxD,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACrC,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;CACzC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sql/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DeepPartial, Repository, DataSource, ObjectLiteral } from "typeorm";
|
|
2
|
+
import { ISqlBaseRepository } from "./i-sql-base-repository";
|
|
3
|
+
export declare class SqlBaseRepository<T extends ObjectLiteral> implements ISqlBaseRepository<T> {
|
|
4
|
+
protected repo: Repository<T>;
|
|
5
|
+
constructor(entity: {
|
|
6
|
+
new (): T;
|
|
7
|
+
}, dataSource: DataSource);
|
|
8
|
+
findOne(where: Partial<T>): Promise<T | null>;
|
|
9
|
+
findAll(where?: Partial<T>): Promise<T[] | null>;
|
|
10
|
+
create(data: DeepPartial<T>): Promise<T | null>;
|
|
11
|
+
update(id: string, data: Partial<T>): Promise<T | null>;
|
|
12
|
+
delete(id: string): Promise<boolean>;
|
|
13
|
+
findById(id: string): Promise<T | null>;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=sql-base-repository.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql-base-repository.d.ts","sourceRoot":"","sources":["../../src/sql/sql-base-repository.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7E,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D,qBACa,iBAAiB,CAAC,CAAC,SAAS,aAAa,CAAE,YAAW,kBAAkB,CAAC,CAAC,CAAC;IACtF,SAAS,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC;gBAElB,MAAM,EAAE;QAAE,QAAQ,CAAC,CAAA;KAAE,EAAE,UAAU,EAAE,UAAU;IAGnD,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAQ7C,OAAO,CAAC,KAAK,GAAE,OAAO,CAAC,CAAC,CAAM,GAAG,OAAO,CAAC,CAAC,EAAE,GAAE,IAAI,CAAC;IAQnD,MAAM,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAW/C,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IASvD,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAStC,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;CAO5C"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { DataSource } from "typeorm";
|
|
11
|
+
import { injectable } from "inversify";
|
|
12
|
+
let SqlBaseRepository = class SqlBaseRepository {
|
|
13
|
+
repo;
|
|
14
|
+
constructor(entity, dataSource) {
|
|
15
|
+
this.repo = dataSource.getRepository(entity);
|
|
16
|
+
}
|
|
17
|
+
async findOne(where) {
|
|
18
|
+
try {
|
|
19
|
+
return await this.repo.findOne({ where });
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async findAll(where = {}) {
|
|
26
|
+
try {
|
|
27
|
+
return await this.repo.find({ where });
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async create(data) {
|
|
34
|
+
try {
|
|
35
|
+
const entity = this.repo.create(data);
|
|
36
|
+
return await this.repo.save(entity);
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
console.log("create", error);
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async update(id, data) {
|
|
44
|
+
try {
|
|
45
|
+
await this.repo.update(id, data);
|
|
46
|
+
return await this.repo.findOne({ where: { id } });
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async delete(id) {
|
|
53
|
+
try {
|
|
54
|
+
const result = await this.repo.delete(id);
|
|
55
|
+
return !!result;
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async findById(id) {
|
|
62
|
+
try {
|
|
63
|
+
return await this.repo.findOne({ where: { id } });
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
SqlBaseRepository = __decorate([
|
|
71
|
+
injectable(),
|
|
72
|
+
__metadata("design:paramtypes", [Object, DataSource])
|
|
73
|
+
], SqlBaseRepository);
|
|
74
|
+
export { SqlBaseRepository };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asyncHandler.d.ts","sourceRoot":"","sources":["../../src/utils/asyncHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEzC,eAAO,MAAM,YAAY,GAAI,IAAI,cAAc,KAAG,cAIjD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bcrypt.d.ts","sourceRoot":"","sources":["../../src/utils/bcrypt.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,aAAa;+BACW,MAAM;iCAIJ,MAAM,eAAe,MAAM;CAGjE,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import bcrypt from "bcryptjs";
|
|
2
|
+
export const bcryptService = {
|
|
3
|
+
securePassword: async (password) => {
|
|
4
|
+
return await bcrypt.hash(password, 10);
|
|
5
|
+
},
|
|
6
|
+
matchPassword: async (passwordOne, passwordTwo) => {
|
|
7
|
+
return await bcrypt.compare(passwordOne, passwordTwo);
|
|
8
|
+
},
|
|
9
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catchAsync.d.ts","sourceRoot":"","sources":["../../src/utils/catchAsync.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE1D,eAAO,MAAM,UAAU,GAAI,IAAI,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,YAAY,KAAK,OAAO,CAAC,GAAG,CAAC,MAC7F,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,YAAY,SAE/C,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class EnvError extends Error {
|
|
2
|
+
readonly key: string;
|
|
3
|
+
constructor(key: string, message?: string);
|
|
4
|
+
}
|
|
5
|
+
export declare function envChecker(value: string | undefined | null, name: string): void;
|
|
6
|
+
export declare function requireEnv(name: string): string;
|
|
7
|
+
export declare function initEnv(options?: {
|
|
8
|
+
path?: string;
|
|
9
|
+
}): import("dotenv").DotenvConfigOutput;
|
|
10
|
+
//# sourceMappingURL=envChecker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envChecker.d.ts","sourceRoot":"","sources":["../../src/utils/envChecker.ts"],"names":[],"mappings":"AAAA,qBAAa,QAAS,SAAQ,KAAK;IACjC,SAAgB,GAAG,EAAE,MAAM,CAAC;gBAChB,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM;CAM1C;AAGD,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAO/E;AAGD,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAI/C;AAGD,wBAAgB,OAAO,CAAC,OAAO,CAAC,EAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,uCAGlD"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export class EnvError extends Error {
|
|
2
|
+
key;
|
|
3
|
+
constructor(key, message) {
|
|
4
|
+
super(message ?? `Environment variable ${key} is required but was not provided.`);
|
|
5
|
+
this.key = key;
|
|
6
|
+
this.name = "EnvError";
|
|
7
|
+
Error.captureStackTrace?.(this, EnvError);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export function envChecker(value, name) {
|
|
11
|
+
if (value === undefined || value === null) {
|
|
12
|
+
throw new EnvError(name);
|
|
13
|
+
}
|
|
14
|
+
if (typeof value === "string" && value.trim() === "") {
|
|
15
|
+
throw new EnvError(name, `Environment variable ${name} is an empty string.`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export function requireEnv(name) {
|
|
19
|
+
const v = process.env[name];
|
|
20
|
+
envChecker(v, name);
|
|
21
|
+
return v;
|
|
22
|
+
}
|
|
23
|
+
export function initEnv(options) {
|
|
24
|
+
const dotenv = require("dotenv");
|
|
25
|
+
return dotenv.config({ path: options?.path });
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AACA,cAAc,cAAc,CAAA;AAC5B,cAAc,UAAU,CAAA;AACxB,cAAc,cAAc,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pick2me/shared",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"description": "Shared library for Pick2Me microservices",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./auth": {
|
|
20
|
+
"types": "./dist/auth/index.d.ts",
|
|
21
|
+
"import": "./dist/auth/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./constants": {
|
|
24
|
+
"types": "./dist/constants/index.d.ts",
|
|
25
|
+
"import": "./dist/constants/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./errors": {
|
|
28
|
+
"types": "./dist/errors/index.d.ts",
|
|
29
|
+
"import": "./dist/errors/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./interfaces": {
|
|
32
|
+
"types": "./dist/interfaces/index.d.ts",
|
|
33
|
+
"import": "./dist/interfaces/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./messaging": {
|
|
36
|
+
"types": "./dist/messaging/index.d.ts",
|
|
37
|
+
"import": "./dist/messaging/index.js"
|
|
38
|
+
},
|
|
39
|
+
"./mongo": {
|
|
40
|
+
"types": "./dist/mongo/index.d.ts",
|
|
41
|
+
"import": "./dist/mongo/index.js"
|
|
42
|
+
},
|
|
43
|
+
"./protos": {
|
|
44
|
+
"types": "./dist/protos/index.d.ts",
|
|
45
|
+
"import": "./dist/protos/index.js"
|
|
46
|
+
},
|
|
47
|
+
"./redis": {
|
|
48
|
+
"types": "./dist/redis/index.d.ts",
|
|
49
|
+
"import": "./dist/redis/index.js"
|
|
50
|
+
},
|
|
51
|
+
"./sql": {
|
|
52
|
+
"types": "./dist/sql/index.d.ts",
|
|
53
|
+
"import": "./dist/sql/index.js"
|
|
54
|
+
},
|
|
55
|
+
"./utils": {
|
|
56
|
+
"types": "./dist/utils/index.d.ts",
|
|
57
|
+
"import": "./dist/utils/index.js"
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "npm run clean && tsc -p tsconfig.json && cpx \"src/protos/**/*\" dist/protos",
|
|
62
|
+
"clean": "rimraf dist",
|
|
63
|
+
"prepare": "npm run build"
|
|
64
|
+
},
|
|
65
|
+
"author": "Sribin",
|
|
66
|
+
"license": "ISC",
|
|
67
|
+
"type": "module",
|
|
68
|
+
"devDependencies": {
|
|
69
|
+
"@types/amqplib": "^0.10.8",
|
|
70
|
+
"@types/bcryptjs": "^2.4.6",
|
|
71
|
+
"@types/express": "^5.0.5",
|
|
72
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
73
|
+
"cpx": "^1.5.0",
|
|
74
|
+
"rimraf": "^5.0.0",
|
|
75
|
+
"ts-node": "^10.9.2",
|
|
76
|
+
"typescript": "^5.9.3"
|
|
77
|
+
},
|
|
78
|
+
"dependencies": {
|
|
79
|
+
"bcryptjs": "^3.0.2",
|
|
80
|
+
"dotenv": "^17.2.3",
|
|
81
|
+
"jsonwebtoken": "^9.0.2"
|
|
82
|
+
},
|
|
83
|
+
"peerDependencies": {
|
|
84
|
+
"@grpc/grpc-js": "^1.14.0",
|
|
85
|
+
"@grpc/proto-loader": "^0.8.0",
|
|
86
|
+
"amqplib": "^0.10.9",
|
|
87
|
+
"express": "^5.1.0",
|
|
88
|
+
"inversify": "^7.10.4",
|
|
89
|
+
"ioredis": "^5.8.2",
|
|
90
|
+
"mongoose": "^8.19.2",
|
|
91
|
+
"typeorm": "^0.3.27"
|
|
92
|
+
},
|
|
93
|
+
"peerDependenciesMeta": {
|
|
94
|
+
"@grpc/grpc-js": {
|
|
95
|
+
"optional": true
|
|
96
|
+
},
|
|
97
|
+
"@grpc/proto-loader": {
|
|
98
|
+
"optional": true
|
|
99
|
+
},
|
|
100
|
+
"amqplib": {
|
|
101
|
+
"optional": true
|
|
102
|
+
},
|
|
103
|
+
"inversify": {
|
|
104
|
+
"optional": true
|
|
105
|
+
},
|
|
106
|
+
"ioredis": {
|
|
107
|
+
"optional": true
|
|
108
|
+
},
|
|
109
|
+
"mongoose": {
|
|
110
|
+
"optional": true
|
|
111
|
+
},
|
|
112
|
+
"typeorm": {
|
|
113
|
+
"optional": true
|
|
114
|
+
},
|
|
115
|
+
"express": {
|
|
116
|
+
"optional": true
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|