feat: 更新 replace-team-qrcode-url 脚本以支持指定的 corpId 处理和过滤逻辑
This commit is contained in:
parent
562655bdea
commit
262f4f4232
@ -94,6 +94,7 @@ function createDefaultOptions() {
|
||||
mongoPass: "",
|
||||
mongoAuthDb: "",
|
||||
corpId: "",
|
||||
corpIdSpecified: false,
|
||||
oldCorpId: "",
|
||||
newCorpId: "",
|
||||
oldPrefix: "https://www.youcan365.com/h5/#/",
|
||||
@ -124,7 +125,10 @@ function parseArgs(argv) {
|
||||
else if (key === "mongouser") opts.mongoUser = value;
|
||||
else if (key === "mongopass") opts.mongoPass = value;
|
||||
else if (key === "mongoauthdb") opts.mongoAuthDb = value;
|
||||
else if (key === "corpid") opts.corpId = value;
|
||||
else if (key === "corpid") {
|
||||
opts.corpId = value;
|
||||
opts.corpIdSpecified = true;
|
||||
}
|
||||
else if (key === "oldcorpid" || key === "sourcecorpid" || key === "targetcorpid") opts.oldCorpId = value;
|
||||
else if (key === "newcorpid" || key === "tokencorpid") opts.newCorpId = value;
|
||||
else if (key === "oldprefix") opts.oldPrefix = value;
|
||||
@ -182,6 +186,7 @@ function parseArgs(argv) {
|
||||
i++;
|
||||
} else if (a === "--corpId") {
|
||||
opts.corpId = takeValue(i);
|
||||
opts.corpIdSpecified = true;
|
||||
i++;
|
||||
} else if (a === "--oldCorpId" || a === "--sourceCorpId" || a === "--targetCorpId") {
|
||||
opts.oldCorpId = takeValue(i);
|
||||
@ -224,6 +229,41 @@ function applyEnvFallbacks(opts) {
|
||||
if (!opts.newPrefix && process.env.NEW_QRCODE_PREFIX) opts.newPrefix = process.env.NEW_QRCODE_PREFIX;
|
||||
}
|
||||
|
||||
function uniqueNonEmptyStrings(values) {
|
||||
const seen = new Set();
|
||||
const result = [];
|
||||
for (const value of values) {
|
||||
if (!isNonEmptyString(value)) continue;
|
||||
const key = value.trim();
|
||||
if (seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
result.push(key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function getFilterCorpIds(opts) {
|
||||
if (opts.corpIdSpecified) {
|
||||
return uniqueNonEmptyStrings([opts.corpId]);
|
||||
}
|
||||
return uniqueNonEmptyStrings([opts.corpId, opts.oldCorpId, opts.newCorpId]);
|
||||
}
|
||||
|
||||
function buildCorpFilter(corpIds) {
|
||||
if (!Array.isArray(corpIds) || corpIds.length === 0) {
|
||||
return {};
|
||||
}
|
||||
if (corpIds.length === 1) {
|
||||
const [corpId] = corpIds;
|
||||
return {
|
||||
$or: [{ corpId }, { corpid: corpId }, { corp_id: corpId }],
|
||||
};
|
||||
}
|
||||
return {
|
||||
$or: [{ corpId: { $in: corpIds } }, { corpid: { $in: corpIds } }, { corp_id: { $in: corpIds } }],
|
||||
};
|
||||
}
|
||||
|
||||
function die(msg) {
|
||||
console.error(msg);
|
||||
process.exit(1);
|
||||
@ -502,7 +542,7 @@ async function main() {
|
||||
可选参数:
|
||||
--dbName <db> 数据库名,默认 corp
|
||||
--collection <name> collection 名,默认 team
|
||||
--corpId <corpId> 仅处理指定机构
|
||||
--corpId <corpId> 仅处理指定机构;未传时默认同时兼容 old/new corpId
|
||||
--oldCorpId <corpId> URL 参数中的旧 corpId,默认取 OLD_CORP_ID / SOURCE_CORP_ID / TARGET_CORP_ID
|
||||
--newCorpId <corpId> URL 参数中的新 corpId,默认取 NEW_CORP_ID / TOKEN_CORP_ID
|
||||
--oldPrefix <url> 旧前缀,默认 https://www.youcan365.com/h5/#/
|
||||
@ -538,11 +578,11 @@ async function main() {
|
||||
const db = client.db(opts.dbName);
|
||||
const teamCol = db.collection(opts.collection);
|
||||
const staffCol = db.collection(opts.staffCollection);
|
||||
const filter = {};
|
||||
if (isNonEmptyString(opts.corpId)) filter.corpId = opts.corpId;
|
||||
const filterCorpIds = getFilterCorpIds(opts);
|
||||
const filter = buildCorpFilter(filterCorpIds);
|
||||
|
||||
console.log(
|
||||
`[step] collection=${opts.dbName}.${opts.collection} corpId=${opts.corpId || "全部"} mode=${opts.dryRun ? "dry-run" : "apply"}`
|
||||
`[step] collection=${opts.dbName}.${opts.collection} corpIds=${filterCorpIds.length ? filterCorpIds.join(",") : "全部"} mode=${opts.dryRun ? "dry-run" : "apply"}`
|
||||
);
|
||||
const teamStats = await processCollectionDocs({
|
||||
collection: teamCol,
|
||||
@ -565,7 +605,7 @@ async function main() {
|
||||
});
|
||||
|
||||
console.log(
|
||||
`[step] collection=${opts.dbName}.${opts.staffCollection} corpId=${opts.corpId || "全部"} mode=${opts.dryRun ? "dry-run" : "apply"}`
|
||||
`[step] collection=${opts.dbName}.${opts.staffCollection} corpIds=${filterCorpIds.length ? filterCorpIds.join(",") : "全部"} mode=${opts.dryRun ? "dry-run" : "apply"}`
|
||||
);
|
||||
const staffStats = await processCollectionDocs({
|
||||
collection: staffCol,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user