feat: Add optional notification email when invite is accepted (#3718)
* feat: Add optional notification email when invite is accepted * Refactor to use beforeSend
This commit is contained in:
@@ -38,6 +38,7 @@ class NotificationSetting extends Model {
|
||||
"documents.publish",
|
||||
"documents.update",
|
||||
"collections.create",
|
||||
"emails.invite_accepted",
|
||||
"emails.onboarding",
|
||||
"emails.features",
|
||||
],
|
||||
@@ -48,12 +49,13 @@ class NotificationSetting extends Model {
|
||||
// getters
|
||||
|
||||
get unsubscribeUrl() {
|
||||
const token = NotificationSetting.getUnsubscribeToken(this.userId);
|
||||
return `${env.URL}/api/notificationSettings.unsubscribe?token=${token}&id=${this.id}`;
|
||||
return `${env.URL}/api/notificationSettings.unsubscribe?token=${this.unsubscribeToken}&id=${this.id}`;
|
||||
}
|
||||
|
||||
get unsubscribeToken() {
|
||||
return NotificationSetting.getUnsubscribeToken(this.userId);
|
||||
const hash = crypto.createHash("sha256");
|
||||
hash.update(`${this.userId}-${env.SECRET_KEY}`);
|
||||
return hash.digest("hex");
|
||||
}
|
||||
|
||||
// associations
|
||||
@@ -71,12 +73,6 @@ class NotificationSetting extends Model {
|
||||
@ForeignKey(() => Team)
|
||||
@Column(DataType.UUID)
|
||||
teamId: string;
|
||||
|
||||
static getUnsubscribeToken = (userId: string) => {
|
||||
const hash = crypto.createHash("sha256");
|
||||
hash.update(`${userId}-${env.SECRET_KEY}`);
|
||||
return hash.digest("hex");
|
||||
};
|
||||
}
|
||||
|
||||
export default NotificationSetting;
|
||||
|
||||
@@ -7,7 +7,6 @@ import {
|
||||
Column,
|
||||
IsIP,
|
||||
IsEmail,
|
||||
HasOne,
|
||||
Default,
|
||||
IsIn,
|
||||
BeforeDestroy,
|
||||
@@ -164,15 +163,14 @@ class User extends ParanoidModel {
|
||||
}
|
||||
|
||||
// associations
|
||||
|
||||
@HasOne(() => User, "suspendedById")
|
||||
@BelongsTo(() => User, "suspendedById")
|
||||
suspendedBy: User | null;
|
||||
|
||||
@ForeignKey(() => User)
|
||||
@Column(DataType.UUID)
|
||||
suspendedById: string | null;
|
||||
|
||||
@HasOne(() => User, "invitedById")
|
||||
@BelongsTo(() => User, "invitedById")
|
||||
invitedBy: User | null;
|
||||
|
||||
@ForeignKey(() => User)
|
||||
@@ -292,11 +290,12 @@ class User extends ParanoidModel {
|
||||
};
|
||||
|
||||
updateSignedIn = (ip: string) => {
|
||||
this.lastSignedInAt = new Date();
|
||||
const now = new Date();
|
||||
this.lastActiveAt = now;
|
||||
this.lastActiveIp = ip;
|
||||
this.lastSignedInAt = now;
|
||||
this.lastSignedInIp = ip;
|
||||
return this.save({
|
||||
hooks: false,
|
||||
});
|
||||
return this.save({ hooks: false });
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -521,6 +520,14 @@ class User extends ParanoidModel {
|
||||
},
|
||||
transaction: options.transaction,
|
||||
}),
|
||||
NotificationSetting.findOrCreate({
|
||||
where: {
|
||||
userId: model.id,
|
||||
teamId: model.teamId,
|
||||
event: "emails.invite_accepted",
|
||||
},
|
||||
transaction: options.transaction,
|
||||
}),
|
||||
]);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user