fix(sms): parse melipayamak xml pattern tags and add search filter in admin panel
This commit is contained in:
1 parent
99e726b08b
commit
2f41d4afe7
2 files changed
+201
-60
No files matched your search
@@ -494,26 +494,30 @@ export class SmsService {
|
||||
*/
|
||||
private parsePatternsXml(xml: string): MeliPayamakPattern[] {
|
||||
const patterns: MeliPayamakPattern[] = [];
|
||||
const itemRegex = /<SharedServiceBody>([\s\S]*?)<\/SharedServiceBody>/gi;
|
||||
// MeliPayamak uses <ShareServiceBody> and child tags <BodyID>, <Title>, <Body>, <BodyStatus>
|
||||
// We match both <ShareServiceBody> and <SharedServiceBody> for backwards/cross compatibility
|
||||
const itemRegex = /<(?:Share|Shared)ServiceBody>([\s\S]*?)<\/(?:Share|Shared)ServiceBody>/gi;
|
||||
let match: RegExpExecArray | null;
|
||||
|
||||
while ((match = itemRegex.exec(xml)) !== null) {
|
||||
const block = match[1];
|
||||
const idMatch = block.match(/<Id>(\d+)<\/Id>/i);
|
||||
const idMatch = block.match(/<(?:BodyID|Id)>(\d+)<\/(?:BodyID|Id)>/i);
|
||||
const titleMatch = block.match(/<Title>([\s\S]*?)<\/Title>/i);
|
||||
const bodyMatch = block.match(/<Body>([\s\S]*?)<\/Body>/i);
|
||||
const statusMatch = block.match(/<Status>(-?\d+)<\/Status>/i);
|
||||
const statusMatch = block.match(/<(?:BodyStatus|Status)>(-?\d+)<\/(?:BodyStatus|Status)>/i);
|
||||
|
||||
if (idMatch) {
|
||||
const id = parseInt(idMatch[1], 10);
|
||||
const title = (titleMatch ? titleMatch[1] : '')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/&/g, '&');
|
||||
.replace(/&/g, '&')
|
||||
.trim();
|
||||
const body = (bodyMatch ? bodyMatch[1] : '')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/&/g, '&');
|
||||
.replace(/&/g, '&')
|
||||
.trim();
|
||||
const status = statusMatch ? parseInt(statusMatch[1], 10) : 0;
|
||||
|
||||
let statusText = 'در انتظار تایید';
|
||||
@@ -611,6 +615,22 @@ export class SmsService {
|
||||
password: config.password,
|
||||
});
|
||||
remotePatterns = this.parsePatternsXml(rawXml);
|
||||
if (remotePatterns.length > 0) {
|
||||
// Cache patterns asynchronously in database
|
||||
this.prisma.setting
|
||||
.upsert({
|
||||
where: { key: 'sms_patterns_cache' },
|
||||
update: { value: remotePatterns as any },
|
||||
create: {
|
||||
key: 'sms_patterns_cache',
|
||||
category: 'sms_patterns',
|
||||
value: remotePatterns as any,
|
||||
},
|
||||
})
|
||||
.catch((err) => {
|
||||
this.logger.warn(`Failed to cache SMS patterns: ${err.message}`);
|
||||
});
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
this.logger.warn(
|
||||
@@ -619,7 +639,7 @@ export class SmsService {
|
||||
}
|
||||
}
|
||||
|
||||
// Load local stored patterns from settings table
|
||||
// Load local stored patterns from settings table (fallback if offline or newly added locally)
|
||||
const storedSetting = await this.prisma.setting.findFirst({
|
||||
where: { category: 'sms_patterns' },
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user