新增批量删除邮件
This commit is contained in:
@@ -11,3 +11,8 @@ app.delete('/allEmail/delete',async (c) => {
|
||||
const list = await emailService.physicsDelete(c, c.req.query());
|
||||
return c.json(result.ok(list));
|
||||
})
|
||||
|
||||
app.delete('/allEmail/batchDelete',async (c) => {
|
||||
await emailService.batchDelete(c, c.req.query());
|
||||
return c.json(result.ok());
|
||||
})
|
||||
|
||||
@@ -70,7 +70,7 @@ const premKey = {
|
||||
'user:set-type': ['/user/setType'],
|
||||
'user:delete': ['/user/delete'],
|
||||
'all-email:query': ['/allEmail/list'],
|
||||
'all-email:delete': ['/allEmail/delete'],
|
||||
'all-email:delete': ['/allEmail/delete','/allEmail/batchDelete'],
|
||||
'setting:query': ['/setting/query'],
|
||||
'setting:set': ['/setting/set', '/setting/setBackground'],
|
||||
'setting:clean': ['/setting/physicsDeleteAll'],
|
||||
|
||||
@@ -160,6 +160,33 @@ const attService = {
|
||||
eq(att.type, attConst.type.ATT)
|
||||
))
|
||||
.all();
|
||||
},
|
||||
|
||||
async deleteByEmailIds(c, emailIds) {
|
||||
|
||||
const queryAttSql = emailIds.map(emailId => c.env.db.prepare(`SELECT key,att_id FROM attachments WHERE email_id = ${emailId} GROUP BY key HAVING COUNT(*) = 1;`))
|
||||
const attListResult = await c.env.db.batch(queryAttSql);
|
||||
|
||||
const delKeyList = attListResult.flatMap(r => r.results.map(row => row.key));
|
||||
|
||||
if (delKeyList.length > 0) {
|
||||
await this.batchDelete(c, delKeyList)
|
||||
}
|
||||
|
||||
const delAttSql = emailIds.map(emailId => c.env.db.prepare(`DELETE FROM attachments WHERE email_id = ${emailId}`))
|
||||
await c.env.db.batch(delAttSql);
|
||||
},
|
||||
|
||||
async batchDelete(c, keys) {
|
||||
if (!keys.length) return;
|
||||
|
||||
const BATCH_SIZE = 1000;
|
||||
|
||||
for (let i = 0; i < keys.length; i += BATCH_SIZE) {
|
||||
const batch = keys.slice(i, i + BATCH_SIZE);
|
||||
await r2Service.delete(c, batch);
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import orm from '../entity/orm';
|
||||
import email from '../entity/email';
|
||||
import { attConst, emailConst, isDel, settingConst } from '../const/entity-const';
|
||||
import { and, desc, eq, gt, inArray, lt, count, asc, sql, ne, or } from 'drizzle-orm';
|
||||
import { and, desc, eq, gt, inArray, lt, count, asc, sql, ne, or, like, lte, gte } from 'drizzle-orm';
|
||||
import { star } from '../entity/star';
|
||||
import settingService from './setting-service';
|
||||
import accountService from './account-service';
|
||||
@@ -614,6 +614,52 @@ const emailService = {
|
||||
isDel: isDel.NORMAL,
|
||||
status: status
|
||||
}).where(eq(email.emailId, emailId)).returning().get();
|
||||
},
|
||||
|
||||
async batchDelete(c, params) {
|
||||
let { sendName, sendEmail, toEmail, subject, startTime, endTime, type } = params
|
||||
|
||||
let right = type === 'left' || type === 'include'
|
||||
let left = type === 'include'
|
||||
|
||||
const conditions = []
|
||||
|
||||
if (sendName) {
|
||||
conditions.push(like(email.name,`${left ? '%' : ''}${sendName}${right ? '%' : ''}`))
|
||||
}
|
||||
|
||||
if (subject) {
|
||||
conditions.push(like(email.subject,`${left ? '%' : ''}${subject}${right ? '%' : ''}`))
|
||||
}
|
||||
|
||||
if (sendEmail) {
|
||||
conditions.push(like(email.sendEmail,`${left ? '%' : ''}${sendEmail}${right ? '%' : ''}`))
|
||||
}
|
||||
|
||||
if (toEmail) {
|
||||
conditions.push(like(email.toEmail,`${left ? '%' : ''}${toEmail}${right ? '%' : ''}`))
|
||||
}
|
||||
|
||||
if (startTime && endTime) {
|
||||
conditions.push(gte(email.createTime,`${startTime}`))
|
||||
conditions.push(lte(email.createTime,`${endTime}`))
|
||||
}
|
||||
|
||||
if (conditions.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const emailIdsRow = await orm(c).select({emailId: email.emailId}).from(email).where(conditions.length > 1 ? and(...conditions) : conditions[0]).all();
|
||||
|
||||
const emailIds = emailIdsRow.map(row => row.emailId);
|
||||
|
||||
if (emailIds.length === 0){
|
||||
return;
|
||||
}
|
||||
|
||||
await attService.deleteByEmailIds(c, emailIds);
|
||||
|
||||
await orm(c).delete(email).where(conditions.length > 1 ? and(...conditions) : conditions[0]).run();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user