template-saas/apps/backend/dist/modules/notifications/services/devices.service.js
rckrdmrd 50a821a415
Some checks failed
CI / Backend CI (push) Has been cancelled
CI / Frontend CI (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / CI Summary (push) Has been cancelled
[SIMCO-V38] feat: Actualizar a SIMCO v3.8.0
- HERENCIA-SIMCO.md actualizado con directivas v3.7 y v3.8
- Actualizaciones de configuracion

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 08:53:08 -06:00

146 lines
6.2 KiB
JavaScript

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var DevicesService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DevicesService = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("@nestjs/typeorm");
const typeorm_2 = require("typeorm");
const entities_1 = require("../entities");
let DevicesService = DevicesService_1 = class DevicesService {
constructor(deviceRepository) {
this.deviceRepository = deviceRepository;
this.logger = new common_1.Logger(DevicesService_1.name);
}
async findByUser(userId, tenantId) {
return this.deviceRepository.find({
where: { user_id: userId, tenant_id: tenantId },
order: { last_used_at: 'DESC' },
});
}
async findActiveByUser(userId, tenantId) {
return this.deviceRepository.find({
where: { user_id: userId, tenant_id: tenantId, is_active: true },
order: { last_used_at: 'DESC' },
});
}
async findById(deviceId, userId, tenantId) {
const device = await this.deviceRepository.findOne({
where: { id: deviceId, user_id: userId, tenant_id: tenantId },
});
if (!device) {
throw new common_1.NotFoundException('Dispositivo no encontrado');
}
return device;
}
async register(userId, tenantId, dto) {
const existing = await this.deviceRepository.findOne({
where: {
user_id: userId,
device_token: dto.deviceToken,
},
});
if (existing) {
existing.is_active = true;
existing.last_used_at = new Date();
existing.device_name = dto.deviceName || existing.device_name;
existing.browser = dto.browser || existing.browser;
existing.browser_version = dto.browserVersion || existing.browser_version;
existing.os = dto.os || existing.os;
existing.os_version = dto.osVersion || existing.os_version;
this.logger.log(`Device ${existing.id} reactivated for user ${userId}`);
return this.deviceRepository.save(existing);
}
const device = this.deviceRepository.create({
user_id: userId,
tenant_id: tenantId,
device_token: dto.deviceToken,
device_type: dto.deviceType || 'web',
device_name: dto.deviceName,
browser: dto.browser,
browser_version: dto.browserVersion,
os: dto.os,
os_version: dto.osVersion,
is_active: true,
last_used_at: new Date(),
});
const saved = await this.deviceRepository.save(device);
this.logger.log(`Device ${saved.id} registered for user ${userId}`);
return saved;
}
async update(deviceId, userId, tenantId, dto) {
const device = await this.findById(deviceId, userId, tenantId);
if (dto.deviceName !== undefined) {
device.device_name = dto.deviceName;
}
if (dto.isActive !== undefined) {
device.is_active = dto.isActive;
}
return this.deviceRepository.save(device);
}
async unregister(deviceId, userId, tenantId) {
const device = await this.findById(deviceId, userId, tenantId);
device.is_active = false;
await this.deviceRepository.save(device);
this.logger.log(`Device ${deviceId} unregistered for user ${userId}`);
}
async delete(deviceId, userId, tenantId) {
const result = await this.deviceRepository.delete({
id: deviceId,
user_id: userId,
tenant_id: tenantId,
});
if (result.affected === 0) {
throw new common_1.NotFoundException('Dispositivo no encontrado');
}
this.logger.log(`Device ${deviceId} deleted for user ${userId}`);
}
async markAsUsed(deviceId) {
await this.deviceRepository.update(deviceId, {
last_used_at: new Date(),
});
}
async markAsInactive(deviceId) {
await this.deviceRepository.update(deviceId, {
is_active: false,
});
this.logger.warn(`Device ${deviceId} marked as inactive (subscription expired)`);
}
async countActiveDevices(userId, tenantId) {
return this.deviceRepository.count({
where: { user_id: userId, tenant_id: tenantId, is_active: true },
});
}
async cleanupInactiveDevices(daysInactive = 90) {
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - daysInactive);
const result = await this.deviceRepository
.createQueryBuilder()
.delete()
.where('is_active = :inactive', { inactive: false })
.andWhere('last_used_at < :cutoff', { cutoff: cutoffDate })
.execute();
if (result.affected && result.affected > 0) {
this.logger.log(`Cleaned up ${result.affected} inactive devices`);
}
return result.affected || 0;
}
};
exports.DevicesService = DevicesService;
exports.DevicesService = DevicesService = DevicesService_1 = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, typeorm_1.InjectRepository)(entities_1.UserDevice)),
__metadata("design:paramtypes", [typeorm_2.Repository])
], DevicesService);
//# sourceMappingURL=devices.service.js.map