Untitled

mail@pastecode.io avatar
unknown
plain_text
15 days ago
2.6 kB
1
Indexable
Never
exports.updateProfile = async (req, res) => {
    const { phone , docs } = req.body;
    try {
        const updatedLogistic = await Logistic.findOneAndUpdate(
            { phone: phone },
            req.body,
            { new: true }
        );
        if (!updatedLogistic) {
            return res.status(404).json({ message: 'Vendor not found' });
        }

        if (docs) {
            document = await Profile(docs)
        }

        const logisticProfile = `${process.env.UPLOAD_URL}` + document.slice(5);
        updatedLogistic.document = logisticProfile
        await updateLogistic.save();
        res.status(200).json({
            message: "Logistic Updated successfully",
            updatedLogistic
        });
    } catch (err) {
        res.status(400).json({ message: err.message });
    }
}

async function Profile(docs) {
    if (!docs) {
        return null;
    }

    const DocsDir = path.join(process.env.FILE_SAVE_PATH, 'LogisticProfile');

    if (!fs.existsSync(DocsDir)) {
        fs.mkdirSync(DocsDir, { recursive: true });
    }

    const picBuffer = Buffer.from(docs, 'base64');
    const picFilename = `${uuidv4()}.jpg`;
    const picPath = path.join(DocsDir, picFilename);

    fs.writeFileSync(picPath, picBuffer);

    return picPath;
}

exports.updateDocs = async (req, res) => {
    const { phone, docs } = req.body;
    try {
        const updatedLogistic = await Logistic.findOneAndUpdate(
            { phone: phone },
            req.body,
            { new: true }
        );
        
        if (!updatedLogistic) {
            return res.status(404).json({ message: 'logistic not found' });
        }

        if (docs) {
            document = await Docs(docs)
        }

        const logisticDoc = `${process.env.UPLOAD_URL}` + document.slice(5);
        updatedLogistic.document = logisticDoc
        await updatedLogistic.save();
        res.status(200).json({
            message: "Logistic Updated successfully",
            updatedLogistic
        });
    } catch (err) {
        res.status(400).json({ message: err.message });
    }
}

async function Docs(docs) {
    if (!docs) {
        return null;
    }

    const DocsDir = path.join(process.env.FILE_SAVE_PATH, 'LogisticDoc');

    if (!fs.existsSync(DocsDir)) {
        fs.mkdirSync(DocsDir, { recursive: true });
    }

    const picBuffer = Buffer.from(docs, 'base64');
    const picFilename = `${uuidv4()}.jpg`;
    const picPath = path.join(DocsDir, picFilename);

    fs.writeFileSync(picPath, picBuffer);

    return picPath;
}
Leave a Comment