feat: implement billing, usage tracking, and administrative geocoding features in API with infrastructure updates
This commit is contained in:
Vendored
+70
-5
@@ -12,6 +12,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
return function (target, key) { decorator(target, key, paramIndex); }
|
||||
};
|
||||
var AuthService_1;
|
||||
var _a, _b;
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AuthService = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
@@ -49,7 +50,7 @@ let AuthService = AuthService_1 = class AuthService {
|
||||
const result = {
|
||||
tenant: apiKey.tenant,
|
||||
apiKey: apiKey,
|
||||
rateLimit: apiKey.rateLimit || 100,
|
||||
rateLimit: apiKey.rateLimit || tenant_entity_1.RATE_LIMITS[apiKey.tenant.plan] || 100,
|
||||
};
|
||||
await this.redisService.set(cacheKey, result, 3600);
|
||||
this.apiKeyRepository.update(apiKey.id, { lastUsedAt: new Date() }).catch(err => this.logger.error(`Failed to update lastUsedAt for API key ${apiKey.id}: ${err.message}`));
|
||||
@@ -87,7 +88,8 @@ let AuthService = AuthService_1 = class AuthService {
|
||||
tenant = await this.tenantRepository.save({
|
||||
name,
|
||||
email,
|
||||
isActive: true
|
||||
isActive: true,
|
||||
plan: tenant_entity_1.TenantPlan.ENTERPRISE,
|
||||
});
|
||||
}
|
||||
const existingKey = await this.apiKeyRepository.findOne({ where: { key: keyString } });
|
||||
@@ -102,14 +104,77 @@ let AuthService = AuthService_1 = class AuthService {
|
||||
});
|
||||
}
|
||||
}
|
||||
async getApiKeys(tenantId) {
|
||||
return this.apiKeyRepository.find({
|
||||
where: { tenantId },
|
||||
order: { createdAt: 'DESC' }
|
||||
});
|
||||
}
|
||||
async createApiKey(tenantId, name, rateLimit, allowedOrigins) {
|
||||
const existingKeysCount = await this.apiKeyRepository.count({ where: { tenantId } });
|
||||
if (existingKeysCount >= 1) {
|
||||
throw new common_1.ConflictException('Limit reached: Only 1 API key allowed per developer currently.');
|
||||
}
|
||||
const key = `in_${(0, crypto_1.createHash)('md5').update(Math.random().toString()).digest('hex').substring(0, 24)}`;
|
||||
const apiKey = this.apiKeyRepository.create({
|
||||
key,
|
||||
secretHash: this.hashSecret(key),
|
||||
name,
|
||||
tenantId,
|
||||
rateLimit: rateLimit || 100,
|
||||
allowedOrigins: allowedOrigins || [],
|
||||
isActive: true
|
||||
});
|
||||
return this.apiKeyRepository.save(apiKey);
|
||||
}
|
||||
async getDefaultTenant() {
|
||||
const tenant = await this.tenantRepository.findOne({ where: {} });
|
||||
if (!tenant)
|
||||
throw new common_1.NotFoundException('No tenants found in system');
|
||||
return tenant;
|
||||
}
|
||||
async findOrCreateByFirebaseUid(uid, email, name, photoUrl) {
|
||||
let tenant = await this.tenantRepository.findOne({ where: { firebaseUid: uid } });
|
||||
if (!tenant) {
|
||||
tenant = await this.tenantRepository.findOne({ where: { email } });
|
||||
if (tenant) {
|
||||
this.logger.log(`Linking existing tenant ${email} to Firebase UID: ${uid}`);
|
||||
tenant.firebaseUid = uid;
|
||||
if (photoUrl)
|
||||
tenant.photoUrl = photoUrl;
|
||||
tenant = await this.tenantRepository.save(tenant);
|
||||
}
|
||||
else {
|
||||
this.logger.log(`Creating new tenant for Firebase user: ${email} (${uid})`);
|
||||
const isAdmin = email === 'hamzaaleghwairyeen@gmail.com' || email === 'hamzadoctor@gmail.com' || email === 'hamzaayedpython@gmail.com';
|
||||
tenant = await this.tenantRepository.save({
|
||||
firebaseUid: uid,
|
||||
email,
|
||||
name,
|
||||
photoUrl,
|
||||
plan: isAdmin ? tenant_entity_1.TenantPlan.ENTERPRISE : tenant_entity_1.TenantPlan.FREE,
|
||||
isActive: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
else {
|
||||
const isAdmin = email === 'hamzaaleghwairyeen@gmail.com' || email === 'hamzadoctor@gmail.com' || email === 'hamzaayedpython@gmail.com';
|
||||
if (isAdmin && tenant.plan !== tenant_entity_1.TenantPlan.ENTERPRISE) {
|
||||
tenant.plan = tenant_entity_1.TenantPlan.ENTERPRISE;
|
||||
await this.tenantRepository.save(tenant);
|
||||
}
|
||||
if (tenant.photoUrl !== photoUrl || tenant.name !== name) {
|
||||
await this.tenantRepository.update(tenant.id, { photoUrl, name });
|
||||
}
|
||||
}
|
||||
return tenant;
|
||||
}
|
||||
};
|
||||
exports.AuthService = AuthService;
|
||||
exports.AuthService = AuthService = AuthService_1 = __decorate([
|
||||
(0, common_1.Injectable)(),
|
||||
__param(0, (0, typeorm_1.InjectRepository)(api_key_entity_1.ApiKey)),
|
||||
__param(1, (0, typeorm_1.InjectRepository)(tenant_entity_1.Tenant)),
|
||||
__metadata("design:paramtypes", [typeorm_2.Repository,
|
||||
typeorm_2.Repository,
|
||||
redis_service_1.RedisService])
|
||||
__metadata("design:paramtypes", [typeof (_a = typeof typeorm_2.Repository !== "undefined" && typeorm_2.Repository) === "function" ? _a : Object, typeof (_b = typeof typeorm_2.Repository !== "undefined" && typeorm_2.Repository) === "function" ? _b : Object, redis_service_1.RedisService])
|
||||
], AuthService);
|
||||
//# sourceMappingURL=auth.service.js.map
|
||||
Reference in New Issue
Block a user