| 1 | import { describe, it, expect, afterAll, beforeEach } from 'vitest'
|
| 2 | import request from 'supertest'
|
| 3 | import { createApp } from '@app/createApp'
|
| 4 | import { buildPrisma, cleanupTestDb, createTestUser, generateTestToken } from '../test-helper'
|
| 5 |
|
| 6 | const app = createApp()
|
| 7 | const db = buildPrisma()
|
| 8 |
|
| 9 | const moment = new Date().toISOString()
|
| 10 |
|
| 11 | afterAll(() => db.$disconnect())
|
| 12 | beforeEach(() => cleanupTestDb(db))
|
| 13 |
|
| 14 | describe('POST /care-actions', () => {
|
| 15 | it('creates an ACTIVITY care action', async () => {
|
| 16 | const user = await createTestUser(db)
|
| 17 | const token = generateTestToken(user.id, user.email)
|
| 18 |
|
| 19 | const res = await request(app)
|
| 20 | .post('/care-actions')
|
| 21 | .set('Authorization', `Bearer ${token}`)
|
| 22 | .send({
|
| 23 | careAction: {
|
| 24 | type: 'ACTIVITY',
|
| 25 | moment,
|
| 26 | activity: { type: 'WALK', duration: 30 },
|
| 27 | },
|
| 28 | })
|
| 29 |
|
| 30 | expect(res.status).toBe(201)
|
| 31 | expect(res.body.careAction.type).toBe('ACTIVITY')
|
| 32 | expect(res.body.careAction.activity.type).toBe('WALK')
|
| 33 | expect(res.body.careAction.activity.duration).toBe(30)
|
| 34 |
|
| 35 | const dbRecord = await db.careAction.findFirst({ where: { userId: user.id } })
|
| 36 | expect(dbRecord).not.toBeNull()
|
| 37 | })
|
| 38 |
|
| 39 | it('creates an APPOINTMENT care action', async () => {
|
| 40 | const user = await createTestUser(db)
|
| 41 | const token = generateTestToken(user.id, user.email)
|
| 42 |
|
| 43 | const res = await request(app)
|
| 44 | .post('/care-actions')
|
| 45 | .set('Authorization', `Bearer ${token}`)
|
| 46 | .send({
|
| 47 | careAction: {
|
| 48 | type: 'APPOINTMENT',
|
| 49 | moment,
|
| 50 | appointment: { type: 'ANALYST', duration: 50, note: 'Primeira sessão' },
|
| 51 | },
|
| 52 | })
|
| 53 |
|
| 54 | expect(res.status).toBe(201)
|
| 55 | expect(res.body.careAction.type).toBe('APPOINTMENT')
|
| 56 | expect(res.body.careAction.appointment.type).toBe('ANALYST')
|
| 57 | expect(res.body.careAction.appointment.note).toBe('Primeira sessão')
|
| 58 | })
|
| 59 |
|
| 60 | it('creates a MEDICINE care action linked to an existing regimen', async () => {
|
| 61 | const user = await createTestUser(db)
|
| 62 | const token = generateTestToken(user.id, user.email)
|
| 63 |
|
| 64 | const regimen = await db.medicineRegimen.create({
|
| 65 | data: {
|
| 66 | userId: user.id,
|
| 67 | name: 'Sertralina',
|
| 68 | dosage: '50mg',
|
| 69 | periodicity: 'DAILY',
|
| 70 | scheduledAt: [],
|
| 71 | },
|
| 72 | })
|
| 73 |
|
| 74 | const res = await request(app)
|
| 75 | .post('/care-actions')
|
| 76 | .set('Authorization', `Bearer ${token}`)
|
| 77 | .send({
|
| 78 | careAction: {
|
| 79 | type: 'MEDICINE',
|
| 80 | moment,
|
| 81 | regimenId: regimen.id,
|
| 82 | },
|
| 83 | })
|
| 84 |
|
| 85 | expect(res.status).toBe(201)
|
| 86 | expect(res.body.careAction.type).toBe('MEDICINE')
|
| 87 | expect(res.body.careAction.medicineLog.regimen.id).toBe(regimen.id)
|
| 88 | })
|
| 89 |
|
| 90 | it('creates a MEDICINE care action with inline medicine, creating a new regimen', async () => {
|
| 91 | const user = await createTestUser(db)
|
| 92 | const token = generateTestToken(user.id, user.email)
|
| 93 |
|
| 94 | const res = await request(app)
|
| 95 | .post('/care-actions')
|
| 96 | .set('Authorization', `Bearer ${token}`)
|
| 97 | .send({
|
| 98 | careAction: {
|
| 99 | type: 'MEDICINE',
|
| 100 | moment,
|
| 101 | medicine: {
|
| 102 | name: 'Melatonina',
|
| 103 | dosage: '5mg',
|
| 104 | periodicity: 'DAILY',
|
| 105 | scheduledAt: ['22:00'],
|
| 106 | },
|
| 107 | },
|
| 108 | })
|
| 109 |
|
| 110 | expect(res.status).toBe(201)
|
| 111 | expect(res.body.careAction.medicineLog.regimen.name).toBe('Melatonina')
|
| 112 |
|
| 113 | const regimenCount = await db.medicineRegimen.count({ where: { userId: user.id } })
|
| 114 | expect(regimenCount).toBe(1)
|
| 115 | })
|
| 116 |
|
| 117 | it('returns 401 without a token', async () => {
|
| 118 | const res = await request(app)
|
| 119 | .post('/care-actions')
|
| 120 | .send({ careAction: { type: 'ACTIVITY', moment, activity: { type: 'WALK' } } })
|
| 121 |
|
| 122 | expect(res.status).toBe(401)
|
| 123 | })
|
| 124 | })
|