Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PB-2228]: feat/real time changes apn #445

Merged
merged 6 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ STORJ_BRIDGE=http://network-api:6382
STRIPE_SK=sk_test_F3Ny2VGUnPga9FtyXkl7mzPc
STRIPE_SK_TEST=sk_test_R3Ny2VG7nPgeJFrtXdt8mrPc
SENDGRID_MODE_SANDBOX=false

# APN
APN_BUNDLE_ID=
APN_TEAM_ID=
APN_KEY_ID=
APN_SECRET=
9 changes: 8 additions & 1 deletion src/app/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import initLimit, { LimitModel } from './limit';
import initTier, { TierModel } from './tier';
import initPaidPlan, { PaidPlansModel } from './paidPlans';
import initTierLimit, { TierLimitsModel } from './tierLimit';
import initUserNotificationTokens, { UserNotificationTokenModel } from './userNotificationTokens';

export type ModelType =
| AppSumoModel
Expand Down Expand Up @@ -53,7 +54,8 @@ export type ModelType =
| PaidPlansModel
| TierLimitsModel
| LimitModel
| TierModel;
| TierModel
| UserNotificationTokenModel;

export default (database: Sequelize) => {
const AppSumo = initAppSumo(database);
Expand Down Expand Up @@ -82,6 +84,7 @@ export default (database: Sequelize) => {
const Tier = initTier(database);
const PaidPlans = initPaidPlan(database);
const TierLimit = initTierLimit(database);
const UserNotificationToken = initUserNotificationTokens(database);

AppSumo.belongsTo(User);

Expand Down Expand Up @@ -132,6 +135,7 @@ export default (database: Sequelize) => {
User.hasMany(PrivateSharingFolder, { foreignKey: 'shared_with', sourceKey: 'uuid' });
User.hasMany(Sharings, { foreignKey: 'owner_id', sourceKey: 'uuid' });
User.hasMany(Sharings, { foreignKey: 'shared_with', sourceKey: 'uuid' });
User.hasMany(UserNotificationToken, { foreignKey: 'userId', sourceKey: 'uuid' });

UserReferral.belongsTo(User, { foreignKey: 'user_id' });
UserReferral.belongsTo(Referral, { foreignKey: 'referral_id' });
Expand Down Expand Up @@ -170,6 +174,8 @@ export default (database: Sequelize) => {
as: 'tiers',
});

UserNotificationToken.belongsTo(User, { foreignKey: 'userId', targetKey: 'uuid' });

return {
[AppSumo.name]: AppSumo,
[Backup.name]: Backup,
Expand Down Expand Up @@ -197,5 +203,6 @@ export default (database: Sequelize) => {
[Limit.name]: Limit,
[PaidPlans.name]: PaidPlans,
[TierLimit.name]: TierLimit,
['userNotificationToken']: UserNotificationToken,
};
};
60 changes: 60 additions & 0 deletions src/app/models/userNotificationTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { DataTypes, ModelDefined, Sequelize } from 'sequelize';

export interface UserNotificationTokenAttributes {
id: string;
userId: string;
token: string;
type: 'macos' | 'android' | 'ios';
createdAt: Date;
updatedAt: Date;
}

export type UserNotificationTokenModel = ModelDefined<UserNotificationTokenAttributes, UserNotificationTokenAttributes>;

export default (database: Sequelize): UserNotificationTokenModel => {
const UserNotificationToken: UserNotificationTokenModel = database.define(
'user_notification_tokens',
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
},
userId: {
type: DataTypes.STRING(36),
allowNull: false,
references: {
model: 'users',
key: 'uuid',
},
},
token: {
type: DataTypes.STRING,
allowNull: false,
},
type: {
type: DataTypes.ENUM('macos', 'android', 'ios'),
allowNull: false,
},
createdAt: {
field: 'created_at',
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
updatedAt: {
field: 'updated_at',
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
},
{
tableName: 'user_notification_tokens',
timestamps: true,
underscored: true,
},
);

return UserNotificationToken;
};
12 changes: 5 additions & 7 deletions src/app/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
import * as AnalyticsService from '../../lib/analytics/AnalyticsService';
import { AuthorizedUser } from './types';
import { default as Notifications } from '../../config/initializers/notifications';
import { default as Apn } from '../../config/initializers/apn';

const Logger = logger.getInstance();

export default (router: Router, service: any, App: any): Router => {
service.Analytics = AnalyticsService;
service.ReCaptcha = ReCaptchaV3;
service.Notifications = Notifications.getInstance();
service.Apn = Apn.getInstance();

AuthRoutes(router, service, App.config);
ActivationRoutes(router, service);
Expand All @@ -56,7 +58,7 @@
const { publicKey, privateKey, revocateKey } = req.body;
const userData: any = (req as AuthorizedUser).user;

let [keys, userBucket] = await Promise.all([

Check failure on line 61 in src/app/routes/routes.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'userBucket' is never reassigned. Use 'const' instead

Check failure on line 61 in src/app/routes/routes.ts

View workflow job for this annotation

GitHub Actions / run-tests (16.x)

'userBucket' is never reassigned. Use 'const' instead
service.KeyServer.getKeys(userData),
service.User.GetUserBucket(userData),
]);
Expand Down Expand Up @@ -107,7 +109,7 @@
if (!userData.root_folder_id) {
throw createHttpError(500, 'Account can not be initialized');
}

const user = {
email: userData.email,
bucket: userData.bucket,
Expand All @@ -121,13 +123,9 @@
res.status(200).send({ user });
} catch (err) {
Logger.error(
`[AUTH/INITIALIZE] ERROR: ${
(err as Error).message
}, BODY ${
JSON.stringify(req.body)
}, STACK: ${
`[AUTH/INITIALIZE] ERROR: ${(err as Error).message}, BODY ${JSON.stringify(req.body)}, STACK: ${
(err as Error).stack
}`
}`,
);

return res.status(500).send({ error: 'Internal Server Error' });
Expand Down
Loading
Loading