add updated field to email verification in db to able to prevent repeated email sending wit verification code

This commit is contained in:
Dario 2020-07-10 10:23:35 +02:00
parent 8801d91ee7
commit 3d6aac8ebe
3 changed files with 15 additions and 7 deletions

View File

@ -5,6 +5,7 @@ CREATE TABLE `email_opt_in` (
`email_opt_in_type_id` int(11) NOT NULL,
`created` datetime NOT NULL DEFAULT current_timestamp(),
`resend_count` int(11) DEFAULT 0,
`updated` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `verification_code` (`verification_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

View File

@ -25,7 +25,9 @@ namespace model {
}
EmailOptIn::EmailOptIn(const EmailOptInTuple& tuple)
: ModelBase(tuple.get<0>()), mUserId(tuple.get<1>()), mEmailVerificationCode(tuple.get<2>()), mType(tuple.get<3>()), mCreated(tuple.get<4>()), mResendCount(tuple.get<5>())
: ModelBase(tuple.get<0>()),
mUserId(tuple.get<1>()), mEmailVerificationCode(tuple.get<2>()), mType(tuple.get<3>()),
mCreated(tuple.get<4>()), mResendCount(tuple.get<5>()), mUpdated(tuple.get<6>())
{
}
@ -45,6 +47,8 @@ namespace model {
<< " (user_id, verification_code, email_opt_in_type_id, resend_count) VALUES(?,?,?,?)"
, use(mUserId), use(mEmailVerificationCode), bind(mType), bind(mResendCount);
unlock();
mUpdated = Poco::DateTime();
mCreated = Poco::DateTime();
return insert;
}
@ -53,9 +57,9 @@ namespace model {
{
Poco::Data::Statement select(session);
select << "SELECT id, user_id, verification_code, email_opt_in_type_id, created, resend_count FROM " << getTableName()
select << "SELECT id, user_id, verification_code, email_opt_in_type_id, created, resend_count, updated FROM " << getTableName()
<< " where " << fieldName << " = ?"
, into(mID), into(mUserId), into(mEmailVerificationCode), into(mType), into(mCreated), into(mResendCount);
, into(mID), into(mUserId), into(mEmailVerificationCode), into(mType), into(mCreated), into(mResendCount), into(mUpdated);
return select;
@ -76,7 +80,7 @@ namespace model {
{
Poco::Data::Statement select(session);
select << "SELECT id, user_id, verification_code, email_opt_in_type_id, created, resend_count FROM " << getTableName()
select << "SELECT id, user_id, verification_code, email_opt_in_type_id, created, resend_count, updated FROM " << getTableName()
<< " where " << fieldName << " = ?";
@ -90,7 +94,7 @@ namespace model {
throw Poco::NullValueException("EmailOptIn::_loadFromDB fieldNames empty or contain only one field");
}
select << "SELECT id, user_id, verification_code, email_opt_in_type_id, created, resend_count FROM " << getTableName()
select << "SELECT id, user_id, verification_code, email_opt_in_type_id, created, resend_count, updated FROM " << getTableName()
<< " where " << fieldNames[0] << " = ? ";
if (conditionType == MYSQL_CONDITION_AND) {
for (int i = 1; i < fieldNames.size(); i++) {
@ -106,7 +110,7 @@ namespace model {
addError(new ParamError("EmailOptIn::_loadFromDB", "condition type not implemented", conditionType));
}
//<< " where " << fieldName << " = ?"
select , into(mID), into(mUserId), into(mEmailVerificationCode), into(mType), into(mCreated), into(mResendCount);
select , into(mID), into(mUserId), into(mEmailVerificationCode), into(mType), into(mCreated), into(mResendCount), into(mUpdated);
return select;
@ -116,6 +120,7 @@ namespace model {
{
Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
mResendCount++;
mUpdated = Poco::DateTime();
return updateIntoDB("resend_count", mResendCount);
}

View File

@ -17,7 +17,7 @@ namespace model {
EMAIL_OPT_IN_REGISTER_DIRECT = 3
};
typedef Poco::Tuple<int, int, Poco::UInt64, int, Poco::DateTime, int> EmailOptInTuple;
typedef Poco::Tuple<int, int, Poco::UInt64, int, Poco::DateTime, int, Poco::DateTime> EmailOptInTuple;
class EmailOptIn : public ModelBase
{
@ -36,6 +36,7 @@ namespace model {
inline int getUserId() const { return mUserId; }
inline int getResendCount() const { Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex); return mResendCount; }
inline Poco::DateTime getCreated() const { return mCreated; }
inline Poco::DateTime getUpdated() const { Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex); return mUpdated; }
inline EmailOptInType getType() const { return static_cast<EmailOptInType>(mType);}
inline void setCode(Poco::UInt64 code) { mEmailVerificationCode = code; }
inline void setUserId(int user_Id) { mUserId = user_Id; }
@ -56,6 +57,7 @@ namespace model {
int mType;
Poco::DateTime mCreated;
int mResendCount;
Poco::DateTime mUpdated;
};