Skip to content

Commit cd67ddf

Browse files
JanKoehnleinroboquat
authored andcommitted
[db] Allow to update PaymentSourceInfos
See #7171
1 parent e6f4bff commit cd67ddf

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

components/gitpod-db/src/accounting-db.spec.db.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { DBAccountEntry } from './typeorm/entity/db-account-entry';
1414
import { TransactionalAccountingDBImpl } from './typeorm/accounting-db-impl';
1515
import { DBWorkspace } from './typeorm/entity/db-workspace';
1616
import { DBWorkspaceInstance } from './typeorm/entity/db-workspace-instance';
17-
import { DBSubscription } from './typeorm/entity/db-subscription';
17+
import { DBPaymentSourceInfo, DBSubscription } from './typeorm/entity/db-subscription';
1818
import { testContainer } from './test-container';
1919
import { TypeORM } from './typeorm/typeorm';
2020
const expect = chai.expect;
@@ -136,6 +136,28 @@ export class AccountingDBSpec {
136136
expectExactlyOne(await this.db.findActiveSubscriptionsForUser(subscription.userId, rightBefore(later)), dbSubscription);
137137
expect(await this.db.findActiveSubscriptionsForUser(subscription.userId, later)).to.be.an('array').and.empty;
138138
}
139+
140+
// see https://github.com/gitpod-io/gitpod/issues/7171
141+
@test public async bug7171() {
142+
const paymentSourceInfo : DBPaymentSourceInfo = {
143+
id: "bar",
144+
resourceVersion: 1,
145+
userId: "foo",
146+
status: "valid",
147+
cardExpiryMonth: 12,
148+
cardExpiryYear: 2021
149+
};
150+
await this.db.storePaymentSourceInfo(paymentSourceInfo);
151+
const paymentSourceInfo2 : DBPaymentSourceInfo = {
152+
id: "bar",
153+
resourceVersion: 1,
154+
userId: "foo",
155+
status: "expiring",
156+
cardExpiryMonth: 12,
157+
cardExpiryYear: 2021
158+
};
159+
await this.db.storePaymentSourceInfo(paymentSourceInfo2);
160+
}
139161
}
140162

141163
const expectExactlyOne = <T>(result: T[], expectation: T) => {

components/gitpod-db/src/typeorm/accounting-db-impl.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,19 @@ export class TypeORMAccountingDBImpl implements AccountingDB {
260260

261261
async storePaymentSourceInfo(info: DBPaymentSourceInfo): Promise<DBPaymentSourceInfo> {
262262
const repo = await this.getPaymentSourceRepo();
263-
return repo.save(info);
263+
// see https://github.com/gitpod-io/gitpod/issues/7171
264+
// TypeORM seems to have problems with number type primary columns
265+
const existing = await repo.findOne({ id: info.id, resourceVersion: info.resourceVersion });
266+
if (existing) {
267+
for (const prop in info) {
268+
if (prop != "resourceVersion") {
269+
(existing as any)[prop] = (info as any)[prop];
270+
}
271+
}
272+
return repo.save(existing);
273+
} else {
274+
return repo.save(info);
275+
}
264276
}
265277
}
266278

0 commit comments

Comments
 (0)