Skip to content

Commit 5683c99

Browse files
committed
Fix queries
1 parent 04e1fe4 commit 5683c99

File tree

7 files changed

+40
-22
lines changed

7 files changed

+40
-22
lines changed

api/pkg/repositories/gorm_message_repository.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ func NewGormMessageRepository(
3636
}
3737

3838
// Index entities.Message between 2 parties
39-
func (repository *gormMessageRepository) Index(ctx context.Context, owner string, contact string, params IndexParams) (*[]entities.Message, error) {
39+
func (repository *gormMessageRepository) Index(ctx context.Context, userID entities.UserID, owner string, contact string, params IndexParams) (*[]entities.Message, error) {
4040
ctx, span := repository.tracer.Start(ctx)
4141
defer span.End()
4242

43-
query := repository.db.WithContext(ctx).Where("owner = ?", owner).Where("contact = ?", contact)
43+
query := repository.db.
44+
WithContext(ctx).
45+
Where("user_id = ?", userID).
46+
Where("owner = ?", owner).
47+
Where("contact = ?", contact)
4448
if len(params.Query) > 0 {
4549
queryPattern := "%" + params.Query + "%"
4650
query.Where("content ILIKE ?", queryPattern)
@@ -69,12 +73,12 @@ func (repository *gormMessageRepository) Store(ctx context.Context, message *ent
6973
}
7074

7175
// Load an entities.Message by ID
72-
func (repository *gormMessageRepository) Load(ctx context.Context, messageID uuid.UUID) (*entities.Message, error) {
76+
func (repository *gormMessageRepository) Load(ctx context.Context, userID entities.UserID, messageID uuid.UUID) (*entities.Message, error) {
7377
ctx, span := repository.tracer.Start(ctx)
7478
defer span.End()
7579

7680
message := new(entities.Message)
77-
err := repository.db.WithContext(ctx).First(message, messageID).Error
81+
err := repository.db.WithContext(ctx).Where("user_id = ?", userID).Where("id = ?", messageID).First(message).Error
7882
if errors.Is(err, gorm.ErrRecordNotFound) {
7983
msg := fmt.Sprintf("message with ID [%s] does not exist", message.ID)
8084
return nil, repository.tracer.WrapErrorSpan(span, stacktrace.PropagateWithCode(err, ErrCodeNotFound, msg))
@@ -102,7 +106,7 @@ func (repository *gormMessageRepository) Update(ctx context.Context, message *en
102106
}
103107

104108
// GetOutstanding fetches messages that still to be sent to the phone
105-
func (repository *gormMessageRepository) GetOutstanding(ctx context.Context, owner string, take int) (*[]entities.Message, error) {
109+
func (repository *gormMessageRepository) GetOutstanding(ctx context.Context, userID entities.UserID, owner string, take int) (*[]entities.Message, error) {
106110
ctx, span := repository.tracer.Start(ctx)
107111
defer span.End()
108112

@@ -111,6 +115,7 @@ func (repository *gormMessageRepository) GetOutstanding(ctx context.Context, own
111115
func(tx *gorm.DB) error {
112116
return tx.WithContext(ctx).Model(messages).
113117
Clauses(clause.Returning{}).
118+
Where("user_id = ?", userID).
114119
Where(
115120
"id IN (?)",
116121
tx.Model(&entities.Message{}).

api/pkg/repositories/gorm_message_thread_repository.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (repository *gormMessageThreadRepository) LoadByOwnerContact(ctx context.Co
7070

7171
err := repository.db.
7272
WithContext(ctx).
73-
Where("user = ?", userID).
73+
Where("user_id = ?", userID).
7474
Where("owner = ?", owner).
7575
Where("contact = ?", contact).
7676
First(thread).
@@ -97,8 +97,9 @@ func (repository *gormMessageThreadRepository) Load(ctx context.Context, userID
9797

9898
err := repository.db.
9999
WithContext(ctx).
100-
Where("user = ?", userID).
100+
Where("user_id = ?", userID).
101101
Where("id = ?", ID).
102+
First(thread).
102103
Error
103104
if errors.Is(err, gorm.ErrRecordNotFound) {
104105
msg := fmt.Sprintf("thread with id [%s] not found", ID)
@@ -120,7 +121,7 @@ func (repository *gormMessageThreadRepository) Index(ctx context.Context, userID
120121

121122
query := repository.db.
122123
WithContext(ctx).
123-
Where("user = ?", userID).
124+
Where("user_id = ?", userID).
124125
Where("owner = ?", owner)
125126

126127
if isArchived {

api/pkg/services/message_service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ func (service *MessageService) StoreSentMessage(ctx context.Context, params Mess
420420
ID: params.ID,
421421
Owner: params.Owner,
422422
Contact: params.Contact,
423+
UserID: params.UserID,
423424
Content: params.Content,
424425
Type: entities.MessageTypeMobileTerminated,
425426
Status: entities.MessageStatusPending,

api/pkg/services/message_thread_service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func (service *MessageThreadService) createThread(ctx context.Context, params Me
114114
ID: uuid.New(),
115115
Owner: params.Owner,
116116
Contact: params.Contact,
117+
UserID: params.UserID,
117118
IsArchived: false,
118119
Color: service.getColor(),
119120
LastMessageContent: params.Content,

web/middleware/user.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export default async function (context: Context) {
88
}
99

1010
const setUser = (context: Context): Promise<User | null> => {
11-
console.log('running user')
1211
return new Promise((resolve, reject) => {
1312
const unsubscribe = (context.app.$fire.auth as Auth).onAuthStateChanged(
1413
async (user) => {

web/pages/threads/_id.vue

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,10 @@ export default Vue.extend({
235235
if (!this.$store.getters.getAuthUser) {
236236
await this.$store.dispatch('setNextRoute', this.$route.path)
237237
await this.$router.push({ name: 'index' })
238+
setTimeout(this.loadData, 2000)
238239
return
239240
}
240-
241-
await this.$store.dispatch('loadPhones')
242-
await this.$store.dispatch('loadThreads')
243-
244-
if (!this.$store.getters.hasThreadId(this.$route.params.id)) {
245-
await this.$router.push({ name: 'threads' })
246-
return
247-
}
248-
249-
await this.$store.dispatch('loadThreadMessages', this.$route.params.id)
250-
this.scrollToElement()
241+
await this.loadData()
251242
},
252243
253244
methods: {
@@ -263,6 +254,19 @@ export default Vue.extend({
263254
) // 5 minutes
264255
},
265256
257+
async loadData() {
258+
await this.$store.dispatch('loadPhones')
259+
await this.$store.dispatch('loadThreads')
260+
261+
if (!this.$store.getters.hasThreadId(this.$route.params.id)) {
262+
await this.$router.push({ name: 'threads' })
263+
return
264+
}
265+
266+
await this.$store.dispatch('loadThreadMessages', this.$route.params.id)
267+
this.scrollToElement()
268+
},
269+
266270
isMT(message: Message): boolean {
267271
return message.type === 'mobile-terminated'
268272
},

web/pages/threads/index.vue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@ export default {
3232
if (!this.$store.getters.getAuthUser) {
3333
await this.$store.dispatch('setNextRoute', this.$route.path)
3434
await this.$router.push({ name: 'index' })
35+
setTimeout(this.loadData, 2000)
3536
return
3637
}
37-
await this.$store.dispatch('loadThreads')
38-
await this.$store.dispatch('loadPhones')
38+
await this.loadData()
39+
},
40+
41+
methods: {
42+
async loadData() {
43+
await this.$store.dispatch('loadThreads')
44+
await this.$store.dispatch('loadPhones')
45+
},
3946
},
4047
}
4148
</script>

0 commit comments

Comments
 (0)