File size: 3,316 Bytes
05cee1f
24727a3
 
 
 
 
 
 
 
 
 
05cee1f
24727a3
 
 
 
 
 
 
 
 
 
47e2259
24727a3
 
 
 
 
 
05cee1f
24727a3
 
 
 
 
 
05cee1f
24727a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05cee1f
24727a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05cee1f
24727a3
 
 
 
 
 
05cee1f
24727a3
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { success, created, paginated } from '../../lib/response.js'
import * as contactService from './contact.service.js'
import { getPagination } from '../../lib/pagination.js'
import { createNotification } from '../notifications/notifications.service.js'
import { NOTIFICATION_TYPES } from '../../lib/constants.js'
import { prisma } from '../../config/database.js'
import { sendContactReplyEmail } from '../../lib/mailer.js'

export async function create(req, res, next) {
  try {
    const message = await contactService.createMessage(req.validatedData)
    created(res, null, 'تم إرسال رسالتك بنجاح، سنتواصل معك قريباً')
  } catch (err) {
    next(err)
  }
}

export async function adminList(req, res, next) {
  try {
    const { page, limit } = getPagination(req.query)
    const status = req.query.status
    const result = await contactService.getAllMessages({ page, limit, status })
    paginated(res, { messages: result.messages, total: result.total, page: result.page, totalPages: result.totalPages }, result.total, result.page, limit)
  } catch (err) { next(err) }
}

export async function adminGetById(req, res, next) {
  try {
    const msg = await contactService.getMessageById(req.params.id)
    success(res, msg)
  } catch (err) { next(err) }
}

export async function adminToggleRead(req, res, next) {
  try {
    const msg = await contactService.toggleRead(req.params.id)
    success(res, null, msg.isRead ? 'تم تحديد كمقروء' : 'تم تحديد كغير مقروء')
  } catch (err) { next(err) }
}

export async function adminToggleStatus(req, res, next) {
  try {
    const { status } = req.body
    const msg = await contactService.updateStatus(req.params.id, status)
    if (status === 'RESOLVED') {
      try {
        const client = await prisma.user.findUnique({ where: { email: msg.email } })
        if (client) {
          await createNotification({
            userId: client.id,
            title: 'تم حل مشكلتك',
            body: `تم حل رسالتك "${msg.subject}"`,
            type: NOTIFICATION_TYPES.CONTACT_RESOLVED,
            link: '/dashboard',
          })
        }
      } catch (_) {}
    }
    success(res, { status: msg.status }, 'تم تحديث الحالة')
  } catch (err) { next(err) }
}

export async function adminReply(req, res, next) {
  try {
    const { content } = req.body
    const reply = await contactService.addReply(req.params.id, content, true)
    const originalMsg = await contactService.getMessageById(req.params.id)
    try {
      const client = await prisma.user.findUnique({ where: { email: originalMsg.email } })
      if (client) {
        await createNotification({
          userId: client.id,
          title: 'تم الرد على رسالتك',
          body: content.substring(0, 100),
          type: NOTIFICATION_TYPES.CONTACT_REPLY,
          link: '/dashboard',
        })
      }
    } catch (_) {}
    sendContactReplyEmail(originalMsg.email, originalMsg.name, content, originalMsg.subject).catch(() => {})
    created(res, reply)
  } catch (err) { next(err) }
}

export async function adminDelete(req, res, next) {
  try {
    await contactService.deleteMessage(req.params.id)
    success(res, null, 'تم حذف الرسالة')
  } catch (err) { next(err) }
}