-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendGridMessage.pm
More file actions
104 lines (81 loc) · 1.9 KB
/
SendGridMessage.pm
File metadata and controls
104 lines (81 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package SendGridMessage;
use strict;
use warnings;
use 5.010;
use Data::Dumper;
use Moo;
use lib "$ENV{HOME}/local_lib/";
use base qw(
SendGridTable
);
my @_fieldOrder = qw(
smtpId
sgMessageId
msgType
stateAbbr
quarterId
districtId
userId
timeStudyId
);
our %_allFields = map { $_ => undef } @_fieldOrder;
sub existsAllField {
my ($class, $key) = @_;
return exists $_allFields{$key};
}
my @_required = qw(
smtpId
sgMessageId
msgType
stateAbbr
);
my $_insertSQL = q{
INSERT INTO tSendGridMessage
(}
. join(', ', @_fieldOrder)
. q{)
VALUES (}
. join(', ', map { '?' } @_fieldOrder)
. q{)
ON DUPLICATE KEY UPDATE
sendGridMessageId = LAST_INSERT_ID(sendGridMessageId),
smtpId = ?,
sgMessageId = ?
};
has [qw( sendGridMessageId )] => (
is => 'ro',
);
has [@_fieldOrder] => (
is => 'rwp',
);
sub upsert {
my ($class, $rParams) = @_;
my $rMsgIds = $rParams->{rMsgIds};
my $rRec = $rParams->{rRec};
my $key = ($rRec->{smtpId} // '') . ($rRec->{sgMessageId} // '');
unless (exists $rMsgIds->{$key}) {
foreach my $k (keys %$rRec) {
my $nk = $k;
if ($nk =~ s/^FB_//) {
if ($class->existsAllField($nk)) {
$rRec->{$nk} = delete $rRec->{$k};
}
}
}
foreach my $k (@_required) {
$rRec->{$k} //= '';
}
my @bind = map { $rRec->{$_} } @_fieldOrder;
push @bind,
($rRec->{smtpId} // ''),
($rRec->{sgMessageId} // '');
my $sth = $class->dbh()->prepare_cached($_insertSQL);
unless ($sth->execute(@bind)) {
warn 'Unable to upsert ' . Dumper ($rRec);
} else {
$rMsgIds->{$key} = $sth->{mysql_insertid};
}
}
return $rMsgIds->{$key} // 0;
}
1;