{"size": 1415, "ext": "js", "max_stars_repo_path": "app-modules/updateCodrop/index.js", "max_stars_repo_name": "xyqfer/youpin", "max_stars_repo_stars_event_min_datetime": "2017-12-04T03:35:58.000Z", "max_stars_repo_stars_event_max_datetime": "2019-04-18T04:19:05.000Z", "max_issues_repo_path": "app-modules/updateCodrop/index.js", "max_issues_repo_name": "xyqfer/youpin", "max_issues_repo_issues_event_min_datetime": "2017-12-04T03:16:41.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T21:18:14.000Z", "max_forks_repo_path": "app-modules/updateCodrop/index.js", "max_forks_repo_name": "xyqfer/youpin", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 203, "max_forks_count": null, "avg_line_length": 25.2678571429, "max_line_length": 126, "alphanum_fraction": 0.4056537102}
'use strict';

module.exports = async () => {
    const getCodropData = require('./getCodropData');
    const {
        db: { getDbData, saveDbData },
        mail: sendMail,
    } = require('app-libs');

    const dbName = 'Codrop';

    try {
        const dbData = await getDbData({
            dbName,
            query: {
                descending: ['updatedAt'],
            },
        });
        const codropData = await getCodropData();

        const newData = codropData.filter((codropItem) => {
            for (let i = 0; i < dbData.length; i++) {
                if (dbData[i].postId === codropItem.postId) {
                    return false;
                }
            }

            return true;
        });

        if (newData.length > 0) {
            saveDbData({
                dbName,
                data: newData.map((item) => ({
                    postId: item.postId,
                })),
            });
            sendMail({
                title: 'Codrop 更新啦',
                data: newData,
                template: ({ url = '', name = '' }) => `<div><a href='${url}' target='_blank'><h4>${name}</h4></a><br></div>`,
                device: 'device2',
                open: 'safari',
                proxy: true,
            });
        }

        return newData;
    } catch (err) {
        console.error(err);
        return {
            success: false,
        };
    }
};
