import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { CreateApplicationResponse } from '~/common/modules/neoleap/dtos/response'; import { Card } from '../entities'; import { CardColors, CardIssuers, CardScheme, CardStatus, CardStatusDescription, CustomerType } from '../enums'; @Injectable() export class CardRepository { constructor(@InjectRepository(Card) private readonly cardRepository: Repository) {} createCard( customerId: string, accountId: string, card: CreateApplicationResponse, cardColor?: CardColors, ): Promise { return this.cardRepository.save( this.cardRepository.create({ customerId: customerId, expiry: card.expiryDate, cardReference: card.cardId, customerType: CustomerType.PARENT, firstSixDigits: card.firstSixDigits, lastFourDigits: card.lastFourDigits, color: cardColor ? cardColor : CardColors.DEEP_MAGENTA, scheme: CardScheme.VISA, issuer: CardIssuers.NEOLEAP, accountId: accountId, vpan: card.vpan, }), ); } getCardById(id: string): Promise { return this.cardRepository.findOne({ where: { id }, relations: ['account'] }); } getCardByReferenceNumber(referenceNumber: string): Promise { return this.cardRepository.findOne({ where: { cardReference: referenceNumber }, relations: ['account'] }); } getCardByVpan(vpan: string): Promise { return this.cardRepository.findOne({ where: { vpan }, relations: ['account'], }); } getCardByCustomerId(customerId: string): Promise { return this.cardRepository.findOne({ where: { customerId }, relations: ['account'], }); } updateCardStatus(id: string, status: CardStatus, statusDescription: CardStatusDescription) { return this.cardRepository.update(id, { status: status, statusDescription: statusDescription, }); } }