FEAT: Atualizado agenda de contatos com canais extras

This commit is contained in:
Rafael Alves Lopes 2026-05-26 11:35:01 -03:00
parent 5a21257191
commit d495698c02
2 changed files with 46 additions and 8 deletions

View File

@ -1,6 +1,17 @@
import { Body, Controller, Get, Param, Put } from '@nestjs/common';
import { CustomerContactsService } from './customer-contacts.service';
interface SaveContactBody {
phone?: string | null;
whatsappPhone?: string | null;
callSmsPhone?: string | null;
email?: string | null;
name?: string | null;
company?: string | null;
note?: string | null;
userId?: number | null;
}
@Controller('contacts')
export class CustomerContactsController {
constructor(private readonly customerContactsService: CustomerContactsService) {}
@ -18,11 +29,13 @@ export class CustomerContactsController {
@Put(':chatId')
saveContact(
@Param('chatId') chatId: string,
@Body() body: { phone?: string | null; name?: string | null; company?: string | null; note?: string | null; userId?: number | null },
@Body() body: SaveContactBody,
) {
return this.customerContactsService.saveContact({
chatId: decodeURIComponent(chatId),
phone: body.phone,
phone: body.whatsappPhone || body.phone,
callSmsPhone: body.callSmsPhone,
email: body.email,
name: body.name,
company: body.company,
note: body.note,

View File

@ -4,6 +4,8 @@ import { DatabaseService } from '../../infra/database/database.service';
interface SaveContactInput {
chatId: string;
phone?: string | null;
callSmsPhone?: string | null;
email?: string | null;
name?: string | null;
company?: string | null;
note?: string | null;
@ -19,6 +21,8 @@ export class CustomerContactsService implements OnModuleInit {
CREATE TABLE IF NOT EXISTS agenda_contatos (
chat_id VARCHAR(255) PRIMARY KEY,
phone VARCHAR(80) NOT NULL,
call_sms_phone VARCHAR(80),
email VARCHAR(255),
name VARCHAR(255),
company VARCHAR(255),
note TEXT,
@ -27,12 +31,17 @@ export class CustomerContactsService implements OnModuleInit {
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);
`);
await this.database.query(`
ALTER TABLE agenda_contatos
ADD COLUMN IF NOT EXISTS call_sms_phone VARCHAR(80),
ADD COLUMN IF NOT EXISTS email VARCHAR(255);
`);
}
async getContact(chatId: string) {
const result = await this.database.query(
`
SELECT chat_id, phone, name, company, note, updated_by_user_id, created_at, updated_at
SELECT chat_id, phone, call_sms_phone, email, name, company, note, updated_by_user_id, created_at, updated_at
FROM agenda_contatos
WHERE chat_id = $1
LIMIT 1
@ -46,10 +55,9 @@ export class CustomerContactsService implements OnModuleInit {
async listContacts() {
const result = await this.database.query(
`
SELECT chat_id, phone, name, company, note, updated_by_user_id, created_at, updated_at
SELECT chat_id, phone, call_sms_phone, email, name, company, note, updated_by_user_id, created_at, updated_at
FROM agenda_contatos
ORDER BY updated_at DESC NULLS LAST, created_at DESC NULLS LAST
LIMIT 80
`,
);
@ -60,20 +68,25 @@ export class CustomerContactsService implements OnModuleInit {
const result = await this.database.query(
`
INSERT INTO agenda_contatos (
chat_id, phone, name, company, note, updated_by_user_id, created_at, updated_at
chat_id, phone, call_sms_phone, email, name, company, note, updated_by_user_id, created_at, updated_at
)
VALUES ($1, $2, $3, $4, $5, $6, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
ON CONFLICT (chat_id) DO UPDATE SET
phone = EXCLUDED.phone,
call_sms_phone = EXCLUDED.call_sms_phone,
email = EXCLUDED.email,
name = EXCLUDED.name,
company = EXCLUDED.company,
note = EXCLUDED.note,
updated_by_user_id = EXCLUDED.updated_by_user_id,
updated_at = CURRENT_TIMESTAMP
RETURNING chat_id, phone, name, company, note, updated_by_user_id, created_at, updated_at
RETURNING chat_id, phone, call_sms_phone, email, name, company, note, updated_by_user_id, created_at, updated_at
`,
[
input.chatId,
this.normalizePhone(input.phone, input.chatId),
this.normalizeOptionalPhone(input.callSmsPhone),
this.normalizeEmail(input.email),
this.normalizeText(input.name),
this.normalizeText(input.company),
this.normalizeText(input.note),
@ -88,6 +101,8 @@ export class CustomerContactsService implements OnModuleInit {
return {
chat_id: chatId,
phone: this.normalizePhone(null, chatId),
call_sms_phone: null,
email: null,
name: null,
company: null,
note: null,
@ -103,6 +118,16 @@ export class CustomerContactsService implements OnModuleInit {
return chatId.endsWith('@lid') ? '' : String(chatId || '').split('@')[0];
}
private normalizeOptionalPhone(phone: string | null | undefined) {
const cleanPhone = String(phone || '').replace(/\D/g, '');
return cleanPhone || null;
}
private normalizeEmail(value?: string | null) {
const email = String(value || '').trim().toLowerCase();
return email || null;
}
private normalizeText(value?: string | null) {
const text = String(value || '').trim();
return text || null;