UNPKG

@bitloops/bl-boilerplate-infra-nest-auth-passport

Version:
76 lines (75 loc) 3.58 kB
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); } }; import { Application, ok, fail, Infra } from '@bitloops/bl-boilerplate-core'; import { Injectable, Inject } from '@nestjs/common'; import { constants } from '@bitloops/bl-boilerplate-infra-postgres'; import { UserRegisteredIntegrationEvent } from './user-registered.integration-event'; import { IntegrationEventBusToken } from '../constants'; let UserPostgresRepository = class UserPostgresRepository { connection; integrationEventBus; tableName = 'users'; constructor(connection, integrationEventBus) { this.connection = connection; this.integrationEventBus = integrationEventBus; } async getByEmail(email) { const result = await this.connection.query(`SELECT * FROM ${this.tableName} WHERE email = $1`, [ email, ]); if (!result.rows.length) { return null; } const user = result.rows[0]; return user; } async checkDoesNotExistAndCreate(user) { // note: we don't try/catch this because if connecting throws an exception // we don't need to dispose of the client (it will be undefined) const client = await this.connection.connect(); try { await client.query('BEGIN'); const userExistsQuery = `SELECT * FROM ${this.tableName} WHERE email = $1`; const res = await client.query(userExistsQuery, [user.email]); if (res.rows.length > 0) { throw new Application.Repo.Errors.Conflict(user.email); } const insertUserText = `INSERT INTO ${this.tableName} (id, email, password) VALUES ($1, $2, $3);`; const { id, email, password } = user; const insertUserValues = [id, email, password]; await this.connection.query(insertUserText, insertUserValues); await client.query('COMMIT'); const event = new UserRegisteredIntegrationEvent({ userId: id, email }); this.integrationEventBus.publish(event); return ok(); } catch (e) { await client.query('ROLLBACK'); if (e instanceof Application.Repo.Errors.Conflict) { return fail(e); } console.log('Error in transaction:', e); return fail(new Application.Repo.Errors.Unexpected(e.message)); } finally { client.release(); } } }; UserPostgresRepository = __decorate([ Injectable(), __param(0, Inject(constants.pg_connection)), __param(1, Inject(IntegrationEventBusToken)), __metadata("design:paramtypes", [Object, Object]) ], UserPostgresRepository); export { UserPostgresRepository };