-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'tony/userreview' of https://github.com/cuappdev/resell-…
…backend into tony/userreview
- Loading branch information
Showing
19 changed files
with
214 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ dist/ | |
node_modules/ | ||
package-lock.json | ||
yarn.lock | ||
*.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { MigrationInterface, QueryRunner, TableColumn } from "typeorm"; | ||
|
||
const TABLE_NAME = "User"; | ||
|
||
export class AddDeviceTokenForUser1682226973547 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
// add device token for user table | ||
await queryRunner.addColumn( | ||
TABLE_NAME, | ||
new TableColumn({ | ||
name: "deviceTokens", | ||
type: "text[]", | ||
default: 'array[]::text[]', | ||
}) | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.dropColumn(TABLE_NAME, "deviceTokens"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {MigrationInterface, QueryRunner, TableColumn} from "typeorm"; | ||
|
||
const TABLE_NAME = "UserSession" | ||
|
||
export class AddDeviceTokenToSession1682461409919 implements MigrationInterface { | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.addColumn( | ||
TABLE_NAME, | ||
new TableColumn({ | ||
name: "deviceToken", | ||
type: "text", | ||
default: "''" | ||
}) | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.dropColumn(TABLE_NAME, "deviceToken"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm'; | ||
|
||
const TABLE_NAME = "User"; | ||
|
||
export class RemoveDeviceTokenForUser1683322987029 implements MigrationInterface { | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.dropColumn(TABLE_NAME, "deviceTokens"); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
// add device token for user table | ||
await queryRunner.addColumn( | ||
TABLE_NAME, | ||
new TableColumn({ | ||
name: "deviceTokens", | ||
type: "text[]", | ||
default: 'array[]::text[]', | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,84 @@ | ||
import { NotFoundError} from 'routing-controllers'; | ||
import { Service } from 'typedi'; | ||
|
||
import { ExpoPushMessage, PushTicket } from '../types'; | ||
import { ExpoPushMessage, PushTicket, FindTokensRequest, NotifSent } from '../types'; | ||
import { Expo } from 'expo-server-sdk'; | ||
|
||
import { UserRepository } from 'src/repositories/UserRepository'; | ||
import Repositories, { TransactionsManager } from '../repositories'; | ||
import { EntityManager } from 'typeorm'; | ||
import { InjectManager } from 'typeorm-typedi-extensions'; | ||
var accessToken = process.env['EXPO_ACCESS_TOKEN'] | ||
const expoServer = new Expo({ accessToken: accessToken }); | ||
|
||
|
||
@Service() | ||
export class NotifService { | ||
// /** | ||
private transactions: TransactionsManager; | ||
|
||
constructor(@InjectManager() entityManager: EntityManager) { | ||
this.transactions = new TransactionsManager(entityManager); | ||
} | ||
|
||
// /** | ||
// * Takes an array of notifications and sends them to users in batches (called chunks) | ||
// * @param {NotifObject[]} notifs an array of notification objects | ||
// * @param {Object} expoServer the server object to connect with | ||
// */ | ||
public sendNotifChunks = async (notifs : ExpoPushMessage[], expoServer : Expo) => { | ||
let chunks = expoServer.chunkPushNotifications(notifs); | ||
let tickets = []; | ||
|
||
for (let chunk of chunks) { | ||
try { | ||
let ticketChunk = await expoServer.sendPushNotificationsAsync(chunk); | ||
// store tickets to check for notif status later | ||
tickets.push(...ticketChunk); | ||
} catch (err) { | ||
console.log("Error while sending notif chunk"); | ||
public sendNotifChunks = async (notifs : ExpoPushMessage[], expoServer : Expo) => { | ||
let chunks = expoServer.chunkPushNotifications(notifs); | ||
let tickets = []; | ||
|
||
for (let chunk of chunks) { | ||
try { | ||
let ticketChunk = await expoServer.sendPushNotificationsAsync(chunk); | ||
// store tickets to check for notif status later | ||
tickets.push(...ticketChunk); | ||
} catch (err) { | ||
console.log("Error while sending notif chunk"); | ||
} | ||
console.log(tickets); | ||
} | ||
console.log(tickets); | ||
} | ||
} | ||
|
||
public sendNotifs = (notif : ExpoPushMessage, json = {}) => { | ||
try { | ||
let notifs : ExpoPushMessage[] = []; | ||
notif.to.forEach(token => { | ||
notifs.push({ | ||
to: notif.to, | ||
sound: notif.sound, | ||
title: notif.title, | ||
body: notif.body, | ||
data: notif.data | ||
}) | ||
public async sendNotifs(request: FindTokensRequest) { | ||
return this.transactions.readWrite(async (transactionalEntityManager) => { | ||
const userRepository = Repositories.user(transactionalEntityManager); | ||
const userSessionRepository = Repositories.session(transactionalEntityManager); | ||
let user = await userRepository.getUserByEmail(request.email); | ||
if (!user) { | ||
throw new NotFoundError("User not found!"); | ||
} | ||
const allDeviceTokens = []; | ||
const allsessions = await userSessionRepository.getSessionsByUserId(user.id); | ||
for (var sess of allsessions) { | ||
if (sess.deviceToken) { | ||
allDeviceTokens.push(sess.deviceToken); } | ||
} | ||
let notif: ExpoPushMessage= | ||
{ | ||
to: allDeviceTokens, | ||
sound: 'default', | ||
title: request.title, | ||
body: request.body, | ||
data: request.data | ||
} | ||
try { | ||
let notifs : ExpoPushMessage[] = []; | ||
notif.to.forEach(token => { | ||
notifs.push({ | ||
to: [token], | ||
sound: notif.sound, | ||
title: notif.title, | ||
body: notif.body, | ||
data: notif.data | ||
}) | ||
}) | ||
this.sendNotifChunks(notifs, expoServer) | ||
} | ||
|
||
// Simply do nothing if the user has no tokens | ||
catch (err) { | ||
console.log(err) } | ||
}) | ||
|
||
this.sendNotifChunks(notifs, expoServer) | ||
} | ||
// Simply do nothing if the user has no tokens | ||
catch (err) { console.log(err) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.