Code Duplication    Length = 35-38 lines in 2 locations

v1/models/admins.js 2 locations

@@ 78-115 (lines=38) @@
75
    },
76
77
    // Delete specific admin
78
    deleteAdmin: async function(res, admin_id, path) {
79
        let adminId = sanitize(admin_id)
80
81
        // Check if the adminId are a valid MongoDB id.
82
        if (!ObjectId.isValid(adminId)) {
83
            return res.status(400).json({
84
                errors: {
85
                    status: 400,
86
                    detail: "The admin_id is not a valid MongoDB id."
87
                }
88
            });
89
        }
90
91
        let client = new MongoClient(mongoURI);
92
        try {
93
            let db = client.db("spark-rentals");
94
            let admins_collection = db.collection("admins");
95
            let admins = await admins_collection.findOne({_id: ObjectId(adminId)});
96
97
            // If nothing in db collection
98
            if (admins === null) {
99
                return res.status(401).json({
100
                    errors: {
101
                        status: 401,
102
                        source: "DELETE /admins" + path,
103
                        title: "Admin not exists in database",
104
                        detail: "The admin dosen't exists in database with the specified admin_id."
105
                    }
106
                });
107
            }
108
109
            // Delete the admin by id
110
            await admins_collection.deleteOne( { "_id" : ObjectId(adminId) } );
111
112
            return res.status(204).send();
113
114
        } catch(e) { return res.status(500).send(); } finally { await client.close(); }
115
    },
116
117
    // Edit specific admin
118
    editAdmin: async function(res, body, path) {
@@ 41-75 (lines=35) @@
38
    },
39
40
    // Get specific admin information
41
    getSpecificAdmin: async function(res, admin_id, path) {
42
        let adminId = sanitize(admin_id);
43
44
        // Check if the adminId are a valid MongoDB id.
45
        if (!ObjectId.isValid(adminId)) {
46
            return res.status(400).json({
47
                errors: {
48
                    status: 400,
49
                    detail: "The admin_id is not a valid MongoDB id."
50
                }
51
            });
52
        }
53
54
        let client = new MongoClient(mongoURI);
55
        try {
56
            let db = client.db("spark-rentals");
57
            let admins_collection = db.collection("admins");
58
            let admin = await admins_collection.findOne({_id: ObjectId(adminId)});
59
60
            // If nothing in collection
61
            if (admin === null) {
62
                return res.status(401).json({
63
                    errors: {
64
                        status: 401,
65
                        source: "GET /admins" + path,
66
                        title: "Admin not exists in database",
67
                        detail: "The admin dosen't exists in database with the specified admin_id."
68
                    }
69
                });
70
            }
71
72
            res.status(200).send({ admin }); // Sends data from the specific admin
73
74
        } catch(e) { return res.status(500).send(); } finally { await client.close(); }
75
    },
76
77
    // Delete specific admin
78
    deleteAdmin: async function(res, admin_id, path) {