Spaces:
Sleeping

File size: 2,723 Bytes
7f81110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Request, Response } from "express";
import { UnitOfWorkFactory } from "../../../../../libs/shared/infrastructure/postgres/unitOfWork/UnitOfWorkFactory";
import { GetInternalCredentialsUseCase } from "../../../application/useCases/GetInternalCredentialsUseCase";
import { GetInternalCredentialsByDomainUseCase } from "../../../application/useCases/GetInternalCredentialsByDomainUseCase";
import { AttachPhoneNumberUseCase } from "../../../application/useCases/AttachPhoneNumberUseCase";

export class InternalCredentialsController {
    constructor(private readonly uowFactory: UnitOfWorkFactory) { }

    async getCredentials(req: Request, res: Response): Promise<void> {
        try {
            const useCase = new GetInternalCredentialsUseCase(this.uowFactory);
            const storeId = req.params.storeId;

            if (!storeId) {
                res.status(400).json({ error: "Store ID is required" });
                return;
            }
            const result = await useCase.execute(storeId.toString());

            if (!result) {
                res.status(404).json({ error: "Not found" });
                return;
            }

            res.json(result);
        } catch (error) {
            res.status(500).json({ error: "Failed to retrieve credentials" });
        }
    }

    async getCredentialsByDomain(req: Request, res: Response): Promise<void> {
        try {
            const useCase = new GetInternalCredentialsByDomainUseCase(this.uowFactory);
            const shopDomain = req.params.shopDomain;

            if (!shopDomain) {
                res.status(400).json({ error: "Shop domain is required" });
                return;
            }

            const result = await useCase.execute(shopDomain.toString());

            if (!result) {
                res.status(404).json({ error: "Not found" });
                return;
            }

            res.json(result);
        } catch (error) {
            res.status(500).json({ error: "Failed to retrieve credentials" });
        }
    }

    async attachPhoneNumber(req: Request, res: Response): Promise<void> {
        try {
            const { storeId, phoneNumberId } = req.body;

            if (!storeId || !phoneNumberId) {
                res.status(400).json({ error: "storeId and phoneNumberId are required" });
                return;
            }

            const useCase = new AttachPhoneNumberUseCase(this.uowFactory);
            await useCase.execute(storeId, phoneNumberId);

            res.json({ message: "Phone number attached successfully" });
        } catch (error: any) {
            res.status(500).json({ error: error.message || "Failed to attach phone number" });
        }
    }
}